From 22eedc8522fbf49c834f1e2e639d3a0072371bf1 Mon Sep 17 00:00:00 2001 From: Roman Alexander Date: Wed, 20 Dec 2023 02:27:03 +0700 Subject: [PATCH] feat: Add support for Redis Sentinels (#216) * Add support for Redis Sentinels * Add some comments --- .../william278/husksync/config/Settings.java | 26 +++++++++++++++++++ .../husksync/redis/RedisManager.java | 21 +++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/net/william278/husksync/config/Settings.java b/common/src/main/java/net/william278/husksync/config/Settings.java index 5223c6c4..4fb01a1b 100644 --- a/common/src/main/java/net/william278/husksync/config/Settings.java +++ b/common/src/main/java/net/william278/husksync/config/Settings.java @@ -134,6 +134,17 @@ public class Settings { @YamlKey("redis.use_ssl") private boolean redisUseSsl = false; + @YamlComment("If you're using Redis Sentinel, specify the master set name. If you don't know what this is, don't change anything here.") + @YamlKey("redis.sentinel.master") + private String redisSentinelMaster = ""; + + @YamlComment("List of host:port pairs") + @YamlKey("redis.sentinel.nodes") + private List redisSentinelNodes = new ArrayList<>(); + + @YamlKey("redis.sentinel.password") + private String redisSentinelPassword = ""; + // Synchronization settings @YamlComment("The mode of data synchronization to use (DELAY or LOCKSTEP). DELAY should be fine for most networks." @@ -324,6 +335,21 @@ public class Settings { return redisUseSsl; } + @NotNull + public String getRedisSentinelMaster() { + return redisSentinelMaster; + } + + @NotNull + public List getRedisSentinelNodes() { + return redisSentinelNodes; + } + + @NotNull + public String getRedisSentinelPassword() { + return redisSentinelPassword; + } + @NotNull public DataSyncer.Mode getSyncMode() { return syncMode; diff --git a/common/src/main/java/net/william278/husksync/redis/RedisManager.java b/common/src/main/java/net/william278/husksync/redis/RedisManager.java index dd569436..2bf3f542 100644 --- a/common/src/main/java/net/william278/husksync/redis/RedisManager.java +++ b/common/src/main/java/net/william278/husksync/redis/RedisManager.java @@ -24,11 +24,9 @@ import net.william278.husksync.data.DataSnapshot; import net.william278.husksync.user.User; import org.jetbrains.annotations.Blocking; import org.jetbrains.annotations.NotNull; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; -import redis.clients.jedis.JedisPubSub; +import redis.clients.jedis.*; import redis.clients.jedis.exceptions.JedisException; +import redis.clients.jedis.util.Pool; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; @@ -47,7 +45,7 @@ public class RedisManager extends JedisPubSub { private final HuskSync plugin; private final String clusterId; - private JedisPool jedisPool; + private Pool jedisPool; private final Map>> pendingRequests; public RedisManager(@NotNull HuskSync plugin) { @@ -71,9 +69,16 @@ public class RedisManager extends JedisPubSub { config.setMaxIdle(0); config.setTestOnBorrow(true); config.setTestOnReturn(true); - this.jedisPool = password.isEmpty() - ? new JedisPool(config, host, port, 0, useSSL) - : new JedisPool(config, host, port, 0, password, useSSL); + Set redisSentinelNodes = new HashSet<>(plugin.getSettings().getRedisSentinelNodes()); + if (redisSentinelNodes.isEmpty()) { + this.jedisPool = password.isEmpty() + ? new JedisPool(config, host, port, 0, useSSL) + : new JedisPool(config, host, port, 0, password, useSSL); + } else { + String sentinelPassword = plugin.getSettings().getRedisSentinelPassword(); + String redisSentinelMaster = plugin.getSettings().getRedisSentinelMaster(); + this.jedisPool = new JedisSentinelPool(redisSentinelMaster, redisSentinelNodes, password.isEmpty() ? null : password, sentinelPassword.isEmpty() ? null : sentinelPassword); + } // Ping the server to check the connection try {