Skip to content

Commit 441ab57

Browse files
Add Redis Sentinel support for messaging (LuckPerms#4244)
1 parent 6f5a8e1 commit 441ab57

12 files changed

Lines changed: 146 additions & 2 deletions

File tree

bukkit/src/main/resources/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ redis:
268268
address: localhost
269269
username: ''
270270
password: ''
271+
# Settings for Redis Sentinel.
272+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
273+
# Port 26379 is used by default for sentinel nodes.
274+
sentinel:
275+
enabled: false
276+
master: mymaster
277+
addresses:
278+
- localhost:26379
279+
username: ''
280+
password: ''
271281

272282
# Settings for Nats.
273283
# Port 4222 is used by default; set address to "host:port" if differs

bungee/src/main/resources/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,16 @@ redis:
266266
address: localhost
267267
username: ''
268268
password: ''
269+
# Settings for Redis Sentinel.
270+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
271+
# Port 26379 is used by default for sentinel nodes.
272+
sentinel:
273+
enabled: false
274+
master: mymaster
275+
addresses:
276+
- localhost:26379
277+
username: ''
278+
password: ''
269279

270280
# Settings for Nats.
271281
# Port 4222 is used by default; set address to "host:port" if differs

common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,31 @@ private ConfigKeys() {}
705705
*/
706706
public static final ConfigKey<Boolean> REDIS_SSL = notReloadable(booleanKey("redis.ssl", false));
707707

708+
/**
709+
* If redis sentinel is enabled
710+
*/
711+
public static final ConfigKey<Boolean> REDIS_SENTINEL_ENABLED = notReloadable(booleanKey("redis.sentinel.enabled", false));
712+
713+
/**
714+
* The name of the redis sentinel master
715+
*/
716+
public static final ConfigKey<String> REDIS_SENTINEL_MASTER = notReloadable(stringKey("redis.sentinel.master", "mymaster"));
717+
718+
/**
719+
* The addresses of the redis sentinel nodes
720+
*/
721+
public static final ConfigKey<List<String>> REDIS_SENTINEL_ADDRESSES = notReloadable(stringListKey("redis.sentinel.addresses", ImmutableList.of()));
722+
723+
/**
724+
* The username to connect to the redis sentinel nodes with, or an empty string if it should use default
725+
*/
726+
public static final ConfigKey<String> REDIS_SENTINEL_USERNAME = notReloadable(stringKey("redis.sentinel.username", ""));
727+
728+
/**
729+
* The password in use by the redis sentinel nodes, or an empty string if there is no password
730+
*/
731+
public static final ConfigKey<String> REDIS_SENTINEL_PASSWORD = notReloadable(stringKey("redis.sentinel.password", ""));
732+
708733
/**
709734
* If nats messaging is enabled
710735
*/

common/src/main/java/me/lucko/luckperms/common/messaging/MessagingFactory.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,21 @@ private class RedisMessengerProvider implements MessengerProvider {
211211
}
212212
boolean ssl = config.get(ConfigKeys.REDIS_SSL);
213213

214-
if (!addresses.isEmpty()) {
214+
boolean sentinelEnabled = config.get(ConfigKeys.REDIS_SENTINEL_ENABLED);
215+
if (sentinelEnabled) {
216+
// redis sentinel
217+
String masterName = config.get(ConfigKeys.REDIS_SENTINEL_MASTER);
218+
List<String> sentinelAddresses = config.get(ConfigKeys.REDIS_SENTINEL_ADDRESSES);
219+
String sentinelUsername = config.get(ConfigKeys.REDIS_SENTINEL_USERNAME);
220+
String sentinelPassword = config.get(ConfigKeys.REDIS_SENTINEL_PASSWORD);
221+
if (sentinelUsername.isEmpty()) {
222+
sentinelUsername = null;
223+
}
224+
if (sentinelPassword.isEmpty()) {
225+
sentinelPassword = null;
226+
}
227+
redis.init(masterName, sentinelAddresses, username, password, ssl, sentinelUsername, sentinelPassword);
228+
} else if (!addresses.isEmpty()) {
215229
// redis cluster
216230
addresses = new ArrayList<>(addresses);
217231
if (address != null) {

common/src/main/java/me/lucko/luckperms/common/messaging/redis/RedisMessenger.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import redis.clients.jedis.JedisCluster;
3737
import redis.clients.jedis.JedisPooled;
3838
import redis.clients.jedis.JedisPubSub;
39+
import redis.clients.jedis.JedisSentineled;
3940
import redis.clients.jedis.Protocol;
4041
import redis.clients.jedis.UnifiedJedis;
4142

@@ -48,6 +49,7 @@
4849
*/
4950
public class RedisMessenger implements Messenger {
5051
private static final String CHANNEL = "luckperms:update";
52+
private static final int SENTINEL_DEFAULT_PORT = 26379;
5153

5254
private final LuckPermsPlugin plugin;
5355
private final IncomingMessageConsumer consumer;
@@ -70,6 +72,13 @@ public void init(String address, String username, String password, boolean ssl)
7072
this.init(new JedisPooled(parseAddress(address), jedisConfig(username, password, ssl)));
7173
}
7274

75+
public void init(String masterName, List<String> sentinelAddresses, String username, String password, boolean ssl, String sentinelUsername, String sentinelPassword) {
76+
Set<HostAndPort> sentinels = sentinelAddresses.stream()
77+
.map(addr -> parseAddress(addr, SENTINEL_DEFAULT_PORT))
78+
.collect(Collectors.toSet());
79+
this.init(new JedisSentineled(masterName, jedisConfig(username, password, ssl), sentinels, jedisConfig(sentinelUsername, sentinelPassword, ssl)));
80+
}
81+
7382
private void init(UnifiedJedis jedis) {
7483
this.jedis = jedis;
7584
this.sub = new Subscription(this);
@@ -86,9 +95,13 @@ private static JedisClientConfig jedisConfig(String username, String password, b
8695
}
8796

8897
private static HostAndPort parseAddress(String address) {
98+
return parseAddress(address, Protocol.DEFAULT_PORT);
99+
}
100+
101+
private static HostAndPort parseAddress(String address, int defaultPort) {
89102
me.lucko.luckperms.common.util.HostAndPort hostAndPort = new me.lucko.luckperms.common.util.HostAndPort(address)
90103
.requireBracketsForIPv6()
91-
.withDefaultPort(Protocol.DEFAULT_PORT);
104+
.withDefaultPort(defaultPort);
92105
String host = hostAndPort.getHost();
93106
int port = hostAndPort.getPort();
94107
return new HostAndPort(host, port);
@@ -162,6 +175,8 @@ private boolean isRedisAlive() {
162175
return !((JedisPooled) jedis).getPool().isClosed();
163176
} else if (jedis instanceof JedisCluster) {
164177
return !((JedisCluster) jedis).getClusterNodes().isEmpty();
178+
} else if (jedis instanceof JedisSentineled) {
179+
return true;
165180
} else {
166181
throw new RuntimeException("Unknown jedis type: " + jedis.getClass().getName());
167182
}

fabric/src/main/resources/luckperms.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ redis {
271271
address = "localhost"
272272
username = ""
273273
password = ""
274+
# Settings for Redis Sentinel.
275+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
276+
# Port 26379 is used by default for sentinel nodes.
277+
sentinel {
278+
enabled = false
279+
master = "mymaster"
280+
addresses = ["localhost:26379"]
281+
username = ""
282+
password = ""
283+
}
274284
}
275285

276286
# Settings for nats.

forge/src/main/resources/luckperms.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ redis {
269269
address = "localhost"
270270
username = ""
271271
password = ""
272+
# Settings for Redis Sentinel.
273+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
274+
# Port 26379 is used by default for sentinel nodes.
275+
sentinel {
276+
enabled = false
277+
master = "mymaster"
278+
addresses = ["localhost:26379"]
279+
username = ""
280+
password = ""
281+
}
272282
}
273283

274284
# Settings for nats.

neoforge/src/main/resources/luckperms.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ redis {
269269
address = "localhost"
270270
username = ""
271271
password = ""
272+
# Settings for Redis Sentinel.
273+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
274+
# Port 26379 is used by default for sentinel nodes.
275+
sentinel {
276+
enabled = false
277+
master = "mymaster"
278+
addresses = ["localhost:26379"]
279+
username = ""
280+
password = ""
281+
}
272282
}
273283

274284
# Settings for nats.

nukkit/src/main/resources/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,16 @@ redis:
263263
address: localhost
264264
username: ''
265265
password: ''
266+
# Settings for Redis Sentinel.
267+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
268+
# Port 26379 is used by default for sentinel nodes.
269+
sentinel:
270+
enabled: false
271+
master: mymaster
272+
addresses:
273+
- localhost:26379
274+
username: ''
275+
password: ''
266276

267277
# Settings for Nats.
268278
# Port 4222 is used by default; set address to "host:port" if differs

sponge/src/main/resources/luckperms.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ redis {
271271
address = "localhost"
272272
username = ""
273273
password = ""
274+
# Settings for Redis Sentinel.
275+
# Sentinel provides high availability for Redis by monitoring master/replica instances.
276+
# Port 26379 is used by default for sentinel nodes.
277+
sentinel {
278+
enabled = false
279+
master = "mymaster"
280+
addresses = ["localhost:26379"]
281+
username = ""
282+
password = ""
283+
}
274284
}
275285

276286
# Settings for nats.

0 commit comments

Comments
 (0)