From 4a849af9b308a76a84036faa48421609f6ee3758 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Fri, 17 May 2024 19:18:03 +0300 Subject: [PATCH] Fixed - org.redisson.spring.data.connection.RedissonConnectionFactory.getSentinelConnection() method throws error on the first offline sentinel #5817 --- .../connection/RedissonConnectionFactory.java | 15 +++++++++------ .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 15 +++++++++------ .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 15 +++++++++------ .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- .../connection/RedissonConnectionFactory.java | 9 ++++++--- .../connection/RedissonConnectionFactory.java | 13 ++++++++----- 14 files changed, 113 insertions(+), 71 deletions(-) diff --git a/redisson-spring-data/redisson-spring-data-16/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-16/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 39f820c9c..be37ce0d0 100644 --- a/redisson-spring-data/redisson-spring-data-16/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-16/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -107,22 +107,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, Initia if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - - SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager()); + + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } } diff --git a/redisson-spring-data/redisson-spring-data-17/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-17/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index c4c875a92..7cee96016 100644 --- a/redisson-spring-data/redisson-spring-data-17/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-17/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -119,22 +119,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, Initia if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } } diff --git a/redisson-spring-data/redisson-spring-data-18/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-18/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index b41cf685c..a71e98e82 100644 --- a/redisson-spring-data/redisson-spring-data-18/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-18/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -126,22 +126,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } } diff --git a/redisson-spring-data/redisson-spring-data-20/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-20/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 50ffde2a4..ed5b3e360 100644 --- a/redisson-spring-data/redisson-spring-data-20/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-20/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -125,22 +125,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - - SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager()); + + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-21/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-21/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 3c966bdf0..ed5b3e360 100644 --- a/redisson-spring-data/redisson-spring-data-21/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-21/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -125,22 +125,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-22/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-22/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-22/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-22/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-23/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-23/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 43481bea2..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-23/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-23/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - - SentinelConnectionManager manager = ((SentinelConnectionManager)((RedissonKeys)redisson.getKeys()).getConnectionManager()); + + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-24/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-24/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-24/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-24/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-25/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-25/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-25/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-25/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-26/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-27/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-30/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-30/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-30/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-30/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-31/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-31/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..00e4c7d39 100644 --- a/redisson-spring-data/redisson-spring-data-31/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-31/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -134,19 +134,22 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override diff --git a/redisson-spring-data/redisson-spring-data-32/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java b/redisson-spring-data/redisson-spring-data-32/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java index 1f76d6097..ecdb97282 100644 --- a/redisson-spring-data/redisson-spring-data-32/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java +++ b/redisson-spring-data/redisson-spring-data-32/src/main/java/org/redisson/spring/data/connection/RedissonConnectionFactory.java @@ -131,22 +131,25 @@ public class RedissonConnectionFactory implements RedisConnectionFactory, if (!redisson.getConfig().isSentinelConfig()) { throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode"); } - + SentinelConnectionManager manager = (SentinelConnectionManager)(((Redisson)redisson).getConnectionManager()); for (RedisClient client : manager.getSentinels()) { - org.redisson.client.RedisConnection connection = client.connect(); + org.redisson.client.RedisConnection connection = null; try { + connection = client.connect(); String res = connection.sync(RedisCommands.PING); if ("pong".equalsIgnoreCase(res)) { return new RedissonSentinelConnection(connection); } } catch (Exception e) { log.warn("Can't connect to " + client, e); - connection.closeAsync(); + if (connection != null) { + connection.closeAsync(); + } } } - - throw new InvalidDataAccessResourceUsageException("Sentinels are not found"); + + throw new InvalidDataAccessResourceUsageException("Sentinels are offline"); } @Override