From e6b8d800bf1885b351636811f3ee7b900238eca7 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 8 Oct 2021 09:38:32 +0200 Subject: [PATCH 1/3] Multithreading for command system * long running commands must not block other commands or processes. to ensure that, everything is now executed multithreaded --- .../tjbot/commands/SlashCommand.java | 12 ++++++++++++ .../tjbot/commands/system/CommandSystem.java | 19 +++++++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java index 6aca0e5bc3..0960b223e7 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java @@ -92,6 +92,10 @@ public interface SlashCommand { * Triggered by the command system when a slash command corresponding to this implementation * (based on {@link #getData()} has been triggered. *

+ * This method may be called multi-threaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

* Details are available in the given event and the event also enables implementations to * respond to it. *

@@ -134,6 +138,10 @@ public interface SlashCommand { * Triggered by the command system when a button corresponding to this implementation (based on * {@link #getData()} has been clicked. *

+ * This method may be called multi-threaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

* Details are available in the given event and the event also enables implementations to * respond to it. *

@@ -150,6 +158,10 @@ public interface SlashCommand { * Triggered by the command system when a selection menu corresponding to this implementation * (based on {@link #getData()} has been clicked. *

+ * This method may be called multi-threaded. In particular, there are no guarantees that it will + * be executed on the same thread repeatedly or on the same thread that other event methods have + * been called on. + *

* Details are available in the given event and the event also enables implementations to * respond to it. *

diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index de122d64a6..329d331a0d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -13,6 +13,7 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; +import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,8 +23,10 @@ import org.togetherjava.tjbot.db.Database; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * The command system is the core of command handling in this application. @@ -40,6 +43,7 @@ public final class CommandSystem extends ListenerAdapter implements SlashCommandProvider { private static final Logger logger = LoggerFactory.getLogger(CommandSystem.class); private static final String RELOAD_COMMAND = "reload"; + private static final int GUILD_SIZE_PARALLEL_THRESHOLD = 10; private final Map nameToSlashCommands; /** @@ -80,7 +84,11 @@ public CommandSystem(@NotNull Database database) { public void onReady(@NotNull ReadyEvent event) { // Register reload on all guilds logger.debug("JDA is ready, registering reload command"); - event.getJDA().getGuildCache().forEach(this::registerReloadCommand); + SnowflakeCacheView guilds = event.getJDA().getGuildCache(); + Stream guildStream = + guilds.size() > GUILD_SIZE_PARALLEL_THRESHOLD ? guilds.parallelStream() + : guilds.stream(); + guildStream.forEach(this::registerReloadCommand); // NOTE We do not have to wait for reload to complete for the command system to be ready // itself logger.debug("Command system is now ready"); @@ -90,21 +98,24 @@ public void onReady(@NotNull ReadyEvent event) { public void onSlashCommand(@NotNull SlashCommandEvent event) { logger.debug("Received slash command '{}' (#{}) on guild '{}'", event.getName(), event.getId(), event.getGuild()); - requireSlashCommand(event.getName()).onSlashCommand(event); + CompletableFuture + .runAsync(() -> requireSlashCommand(event.getName()).onSlashCommand(event)); } @Override public void onButtonClick(@NotNull ButtonClickEvent event) { logger.debug("Received button click '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); - forwardComponentCommand(event, SlashCommand::onButtonClick); + CompletableFuture + .runAsync(() -> forwardComponentCommand(event, SlashCommand::onButtonClick)); } @Override public void onSelectionMenu(@NotNull SelectionMenuEvent event) { logger.debug("Received selection menu event '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); - forwardComponentCommand(event, SlashCommand::onSelectionMenu); + CompletableFuture + .runAsync(() -> forwardComponentCommand(event, SlashCommand::onSelectionMenu)); } private void registerReloadCommand(@NotNull Guild guild) { From df25b56660044b57139026c7f1b89f2013cf08e8 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 8 Oct 2021 10:27:56 +0200 Subject: [PATCH 2/3] Renamed guild threshold GUILD_SIZE_PARALLEL_THRESHOLD -> GUILDS_AMOUNT_PARALLEL_THRESHOLD --- .../org/togetherjava/tjbot/commands/system/CommandSystem.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index 329d331a0d..6b7ad446f1 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -43,7 +43,7 @@ public final class CommandSystem extends ListenerAdapter implements SlashCommandProvider { private static final Logger logger = LoggerFactory.getLogger(CommandSystem.class); private static final String RELOAD_COMMAND = "reload"; - private static final int GUILD_SIZE_PARALLEL_THRESHOLD = 10; + private static final int GUILDS_AMOUNT_PARALLEL_THRESHOLD = 10; private final Map nameToSlashCommands; /** @@ -86,7 +86,7 @@ public void onReady(@NotNull ReadyEvent event) { logger.debug("JDA is ready, registering reload command"); SnowflakeCacheView guilds = event.getJDA().getGuildCache(); Stream guildStream = - guilds.size() > GUILD_SIZE_PARALLEL_THRESHOLD ? guilds.parallelStream() + guilds.size() > GUILDS_AMOUNT_PARALLEL_THRESHOLD ? guilds.parallelStream() : guilds.stream(); guildStream.forEach(this::registerReloadCommand); // NOTE We do not have to wait for reload to complete for the command system to be ready From 1c352b120bac8a299e6903a640deabfb23db8495 Mon Sep 17 00:00:00 2001 From: Zabuzard Date: Fri, 8 Oct 2021 11:54:01 +0200 Subject: [PATCH 3/3] Replaced FJP by CTP --- .../tjbot/commands/system/CommandSystem.java | 25 ++++++++----------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index 6b7ad446f1..fd60e4c283 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -13,7 +13,6 @@ import net.dv8tion.jda.api.interactions.commands.Command; import net.dv8tion.jda.api.interactions.components.ComponentInteraction; import net.dv8tion.jda.api.requests.ErrorResponse; -import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,10 +22,10 @@ import org.togetherjava.tjbot.db.Database; import java.util.*; -import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.function.Function; import java.util.stream.Collectors; -import java.util.stream.Stream; /** * The command system is the core of command handling in this application. @@ -43,7 +42,7 @@ public final class CommandSystem extends ListenerAdapter implements SlashCommandProvider { private static final Logger logger = LoggerFactory.getLogger(CommandSystem.class); private static final String RELOAD_COMMAND = "reload"; - private static final int GUILDS_AMOUNT_PARALLEL_THRESHOLD = 10; + private static final ExecutorService COMMAND_SERVICE = Executors.newCachedThreadPool(); private final Map nameToSlashCommands; /** @@ -84,11 +83,9 @@ public CommandSystem(@NotNull Database database) { public void onReady(@NotNull ReadyEvent event) { // Register reload on all guilds logger.debug("JDA is ready, registering reload command"); - SnowflakeCacheView guilds = event.getJDA().getGuildCache(); - Stream guildStream = - guilds.size() > GUILDS_AMOUNT_PARALLEL_THRESHOLD ? guilds.parallelStream() - : guilds.stream(); - guildStream.forEach(this::registerReloadCommand); + event.getJDA() + .getGuildCache() + .forEach(guild -> COMMAND_SERVICE.execute(() -> registerReloadCommand(guild))); // NOTE We do not have to wait for reload to complete for the command system to be ready // itself logger.debug("Command system is now ready"); @@ -98,24 +95,22 @@ public void onReady(@NotNull ReadyEvent event) { public void onSlashCommand(@NotNull SlashCommandEvent event) { logger.debug("Received slash command '{}' (#{}) on guild '{}'", event.getName(), event.getId(), event.getGuild()); - CompletableFuture - .runAsync(() -> requireSlashCommand(event.getName()).onSlashCommand(event)); + COMMAND_SERVICE.execute(() -> requireSlashCommand(event.getName()).onSlashCommand(event)); } @Override public void onButtonClick(@NotNull ButtonClickEvent event) { logger.debug("Received button click '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); - CompletableFuture - .runAsync(() -> forwardComponentCommand(event, SlashCommand::onButtonClick)); + COMMAND_SERVICE.execute(() -> forwardComponentCommand(event, SlashCommand::onButtonClick)); } @Override public void onSelectionMenu(@NotNull SelectionMenuEvent event) { logger.debug("Received selection menu event '{}' (#{}) on guild '{}'", event.getComponentId(), event.getId(), event.getGuild()); - CompletableFuture - .runAsync(() -> forwardComponentCommand(event, SlashCommand::onSelectionMenu)); + COMMAND_SERVICE + .execute(() -> forwardComponentCommand(event, SlashCommand::onSelectionMenu)); } private void registerReloadCommand(@NotNull Guild guild) {