From c4bd65782693c082d68bc7bebf859ff3a2150ce6 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 19 Nov 2019 15:18:47 +0000 Subject: [PATCH] Changes to messaging service setup --- bukkit/src/main/resources/config.yml | 4 +- bungee/src/main/resources/config.yml | 4 +- .../luckperms/common/config/ConfigKeys.java | 2 +- .../common/messaging/MessagingFactory.java | 46 +++++++++++++------ .../luckperms/common/storage/Storage.java | 11 +++++ .../implementation/split/SplitStorage.java | 6 ++- .../common/verbose/VerboseFilter.java | 18 ++++---- .../common/verbose/VerboseListener.java | 10 +--- nukkit/src/main/resources/config.yml | 4 +- sponge/src/main/resources/luckperms.conf | 4 +- velocity/src/main/resources/config.yml | 4 +- 11 files changed, 69 insertions(+), 44 deletions(-) diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index d7fc9e747..58f490e15 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -213,8 +213,8 @@ watch-files: true # installed. # => redis Uses Redis pub-sub to push changes. Your server connection info must be configured # below. -# => none Disables the service. -messaging-service: none +# => auto Attempts to automatically setup a messaging service using redis or sql. +messaging-service: auto # If LuckPerms should automatically push updates after a change has been made with a command. auto-push-updates: true diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index b09d583cb..bfca2146d 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -211,8 +211,8 @@ watch-files: true # configured below. # => redisbungee Uses Redis pub-sub to push changes, using the RedisBungee API. You need to have # the RedisBungee plugin installed. -# => none Disables the service. -messaging-service: none +# => auto Attempts to automatically setup a messaging service using redis or sql. +messaging-service: auto # If LuckPerms should automatically push updates after a change has been made with a command. auto-push-updates: true diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index e02cf786b..e063128c5 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -485,7 +485,7 @@ public final class ConfigKeys { /** * The name of the messaging service in use, or "none" if not enabled */ - public static final ConfigKey MESSAGING_SERVICE = enduringKey(lowercaseStringKey("messaging-service", "none")); + public static final ConfigKey MESSAGING_SERVICE = enduringKey(lowercaseStringKey("messaging-service", "auto")); /** * If updates should be automatically pushed by the messaging service diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/MessagingFactory.java b/common/src/main/java/me/lucko/luckperms/common/messaging/MessagingFactory.java index 8feb4595d..3745aa28c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/MessagingFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/MessagingFactory.java @@ -25,12 +25,11 @@ package me.lucko.luckperms.common.messaging; -import com.google.common.base.Preconditions; - import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.messaging.redis.RedisMessenger; import me.lucko.luckperms.common.messaging.sql.SqlMessenger; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; +import me.lucko.luckperms.common.storage.implementation.StorageImplementation; import me.lucko.luckperms.common.storage.implementation.sql.SqlStorage; import me.lucko.luckperms.common.storage.implementation.sql.connection.hikari.MariaDbConnectionFactory; import me.lucko.luckperms.common.storage.implementation.sql.connection.hikari.MySqlConnectionFactory; @@ -53,19 +52,29 @@ public class MessagingFactory

{ } public final InternalMessagingService getInstance() { - String messagingType = this.plugin.getConfiguration().get(ConfigKeys.MESSAGING_SERVICE).toLowerCase(); - if (messagingType.equals("none") && this.plugin.getConfiguration().get(ConfigKeys.REDIS_ENABLED)) { - messagingType = "redis"; + String messagingType = this.plugin.getConfiguration().get(ConfigKeys.MESSAGING_SERVICE); + if (messagingType.equals("none")) { + messagingType = "auto"; } - if (messagingType.equals("none") && this.plugin.getStorage().getImplementation() instanceof SqlStorage) { - SqlStorage dao = (SqlStorage) this.plugin.getStorage().getImplementation(); - if (dao.getConnectionFactory() instanceof MySqlConnectionFactory || dao.getConnectionFactory() instanceof MariaDbConnectionFactory) { - messagingType = "sql"; + // attempt to detect "auto" messaging service type. + if (messagingType.equals("auto")) { + if (this.plugin.getConfiguration().get(ConfigKeys.REDIS_ENABLED)) { + messagingType = "redis"; + } else { + for (StorageImplementation implementation : this.plugin.getStorage().getImplementations()) { + if (implementation instanceof SqlStorage) { + SqlStorage sql = (SqlStorage) implementation; + if (sql.getConnectionFactory() instanceof MySqlConnectionFactory || sql.getConnectionFactory() instanceof MariaDbConnectionFactory) { + messagingType = "sql"; + break; + } + } + } } } - if (messagingType.equals("none") || messagingType.equals("notsql")) { + if (messagingType.equals("auto") || messagingType.equals("notsql")) { return null; } @@ -126,12 +135,19 @@ public class MessagingFactory

{ @Override public @NonNull Messenger obtain(@NonNull IncomingMessageConsumer incomingMessageConsumer) { - SqlStorage dao = (SqlStorage) getPlugin().getStorage().getImplementation(); - Preconditions.checkState(dao.getConnectionFactory() instanceof MySqlConnectionFactory || dao.getConnectionFactory() instanceof MariaDbConnectionFactory, "not a supported sql type"); + for (StorageImplementation implementation : getPlugin().getStorage().getImplementations()) { + if (implementation instanceof SqlStorage) { + SqlStorage storage = (SqlStorage) implementation; + if (storage.getConnectionFactory() instanceof MySqlConnectionFactory || storage.getConnectionFactory() instanceof MariaDbConnectionFactory) { + // found an implementation match! + SqlMessenger sql = new SqlMessenger(getPlugin(), storage, incomingMessageConsumer); + sql.init(); + return sql; + } + } + } - SqlMessenger sql = new SqlMessenger(getPlugin(), dao, incomingMessageConsumer); - sql.init(); - return sql; + throw new IllegalStateException("Can't find a supported sql storage implementation"); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/Storage.java b/common/src/main/java/me/lucko/luckperms/common/storage/Storage.java index 3bfc3032d..bab967018 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/Storage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/Storage.java @@ -35,6 +35,7 @@ import me.lucko.luckperms.common.model.Track; import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.implementation.StorageImplementation; +import me.lucko.luckperms.common.storage.implementation.split.SplitStorage; import me.lucko.luckperms.common.util.ThrowingRunnable; import net.luckperms.api.actionlog.Action; @@ -43,6 +44,8 @@ import net.luckperms.api.event.cause.DeletionCause; import net.luckperms.api.model.PlayerSaveResult; import net.luckperms.api.node.HeldNode; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -68,6 +71,14 @@ public class Storage { return this.implementation; } + public Collection getImplementations() { + if (this.implementation instanceof SplitStorage) { + return ((SplitStorage) this.implementation).getImplementations().values(); + } else { + return Collections.singleton(this.implementation); + } + } + private CompletableFuture makeFuture(Callable supplier) { return CompletableFuture.supplyAsync(() -> { try { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/split/SplitStorage.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/split/SplitStorage.java index b72997838..3585350e2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/split/SplitStorage.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/split/SplitStorage.java @@ -58,7 +58,11 @@ public class SplitStorage implements StorageImplementation { this.implementations = ImmutableMap.copyOf(implementations); this.types = ImmutableMap.copyOf(types); } - + + public Map getImplementations() { + return this.implementations; + } + private StorageImplementation implFor(SplitStorageType type) { return this.implementations.get(this.types.get(type)); } diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseFilter.java b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseFilter.java index 8ee6a62af..8462a1f3e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseFilter.java +++ b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseFilter.java @@ -38,16 +38,18 @@ import me.lucko.luckperms.common.verbose.expression.BooleanExpressionCompiler.Pa * evaluations should be relatively fast.

*/ public final class VerboseFilter { + private final String expression; private final AST ast; - public VerboseFilter(String filterExpression) throws InvalidFilterException { - if (filterExpression.isEmpty()) { + public VerboseFilter(String expression) throws InvalidFilterException { + this.expression = expression; + if (expression.isEmpty()) { this.ast = AST.ALWAYS_TRUE; } else { try { - this.ast = BooleanExpressionCompiler.compile(filterExpression); + this.ast = BooleanExpressionCompiler.compile(expression); } catch (LexerException | ParserException e) { - throw new InvalidFilterException("Exception occurred whilst generating an expression for '" + filterExpression + "'", e); + throw new InvalidFilterException("Exception occurred whilst generating an expression for '" + expression + "'", e); } } } @@ -59,10 +61,6 @@ public final class VerboseFilter { * @return if the check data passes the filter */ public boolean evaluate(VerboseEvent data) { - if (isBlank()) { - return true; - } - try { return this.ast.eval(data); } catch (Exception e) { @@ -75,4 +73,8 @@ public final class VerboseFilter { return this.ast == AST.ALWAYS_TRUE; } + @Override + public String toString() { + return isBlank() ? "any" : this.expression; + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java index a7d057167..d2bed6afc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java +++ b/common/src/main/java/me/lucko/luckperms/common/verbose/VerboseListener.java @@ -258,14 +258,6 @@ public class VerboseListener { String endDate = DATE_FORMAT.format(new Date(now)); long secondsTaken = (now - this.startTime) / 1000L; String duration = DurationFormatter.CONCISE.format(secondsTaken); - - String filter; - if (this.filter.isBlank()){ - filter = "any"; - } else { - filter = this.filter.toString(); - } - boolean truncated = this.matchedCounter.get() > this.results.size(); JObject metadata = new JObject() @@ -280,7 +272,7 @@ public class VerboseListener { .add("name", this.notifiedSender.getNameWithLocation()) .add("uuid", this.notifiedSender.getUniqueId().toString()) ) - .add("filter", filter) + .add("filter", this.filter.toString()) .add("truncated", truncated); JArray data = new JArray(); diff --git a/nukkit/src/main/resources/config.yml b/nukkit/src/main/resources/config.yml index 9e8f8a7e2..c9e6c739f 100644 --- a/nukkit/src/main/resources/config.yml +++ b/nukkit/src/main/resources/config.yml @@ -208,8 +208,8 @@ watch-files: true # option is set to 'none' and SQL storage is in use. Set to 'notsql' to disable this. # => redis Uses Redis pub-sub to push changes. Your server connection info must be configured # below. -# => none Disables the service. -messaging-service: none +# => auto Attempts to automatically setup a messaging service using redis or sql. +messaging-service: auto # If LuckPerms should automatically push updates after a change has been made with a command. auto-push-updates: true diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index 195bb893c..f3ac4f5b8 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -216,8 +216,8 @@ watch-files = true # Won't work if you have more than one proxy. # => redis Uses Redis pub-sub to push changes. Your server connection info must be configured # below. -# => none Disables the service. -messaging-service = "none" +# => auto Attempts to automatically setup a messaging service using redis or sql. +messaging-service = "auto" # If LuckPerms should automatically push updates after a change has been made with a command. auto-push-updates = true diff --git a/velocity/src/main/resources/config.yml b/velocity/src/main/resources/config.yml index ba67c0739..e10cae5e5 100644 --- a/velocity/src/main/resources/config.yml +++ b/velocity/src/main/resources/config.yml @@ -202,8 +202,8 @@ watch-files: true # servers. Won't work if you have more than one Velocity proxy. # => redis Uses Redis pub-sub to push changes. Your server connection info must be # configured below. -# => none Disables the service. -messaging-service: none +# => auto Attempts to automatically setup a messaging service using redis or sql. +messaging-service: auto # If LuckPerms should automatically push updates after a change has been made with a command. auto-push-updates: true