Skip to content

Commit

Permalink
Fixed - org.redisson.spring.data.connection.RedissonConnectionFactory…
Browse files Browse the repository at this point in the history
….getSentinelConnection() method throws error on the first offline sentinel #5817
  • Loading branch information
Nikita Koksharov committed May 17, 2024
1 parent 6bfd7c9 commit 4a849af
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,25 @@ public RedisSentinelConnection getSentinelConnection() {
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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,25 @@ public RedisSentinelConnection getSentinelConnection() {
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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,25 @@ public RedisSentinelConnection getSentinelConnection() {
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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,25 @@ public RedisSentinelConnection getSentinelConnection() {
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
Expand Down

0 comments on commit 4a849af

Please sign in to comment.