From cfc88cce3844a3427e291ab79bf97791097960e7 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 22 Oct 2023 22:18:16 +0800 Subject: bugfix: not work in non-debug server environment --- src/main/java/com/keuin/blame/SubmitWorker.java | 15 ++++- .../com/keuin/blame/config/DatabaseConfig.java | 78 +++++++++++++++++++--- .../java/com/keuin/blame/lookup/QueryExecutor.java | 4 +- .../java/com/keuin/blame/util/DatabaseUtil.java | 8 +-- src/main/java/com/keuin/blame/util/PrintUtil.java | 2 +- 5 files changed, 90 insertions(+), 17 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/keuin/blame/SubmitWorker.java b/src/main/java/com/keuin/blame/SubmitWorker.java index 2bca5e5..8eba940 100644 --- a/src/main/java/com/keuin/blame/SubmitWorker.java +++ b/src/main/java/com/keuin/blame/SubmitWorker.java @@ -21,7 +21,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class SubmitWorker { public static final SubmitWorker INSTANCE = new SubmitWorker(); - private final Logger logger = LogManager.getLogger(); + private final Logger logger = LogManager.getLogger(SubmitWorker.class); private final BlockingQueue queue = new ArrayBlockingQueue<>(1048576); private final Thread thread = new Thread(SubmitWorker.this::run); private final AtomicBoolean isStopped = new AtomicBoolean(false); @@ -33,6 +33,7 @@ public class SubmitWorker { private SubmitWorker() { thread.setUncaughtExceptionHandler((t, e) -> logger.error(String.format("Exception in thread %s: %s", t.getName(), e))); + thread.setName(SubmitWorker.class.getSimpleName()); thread.start(); } @@ -117,6 +118,15 @@ public class SubmitWorker { } private void run() { + try { + logger.info("ClickHouse writer thread started."); + doRun(); + } finally { + logger.info("ClickHouse writer thread stopped."); + } + } + + private void doRun() { var server = DatabaseUtil.getServer(); var batchBuffer = new ArrayList(batchSize); boolean writeImmediately = false; @@ -126,7 +136,7 @@ public class SubmitWorker { writeLoop: while (true) { var req = client.read(server).write() - .table(DatabaseUtil.DB_CONFIG.table()) + .table(DatabaseUtil.DB_CONFIG.getTable()) .format(ClickHouseFormat.RowBinary) // .option(ClickHouseClientOption.ASYNC, false) ; @@ -137,6 +147,7 @@ public class SubmitWorker { } case RECONNECT -> { writeImmediately = true; + logger.info("Reconnecting to ClickHouse..."); break writeLoop; } case FINISH -> { diff --git a/src/main/java/com/keuin/blame/config/DatabaseConfig.java b/src/main/java/com/keuin/blame/config/DatabaseConfig.java index e7d3189..6f1cf17 100644 --- a/src/main/java/com/keuin/blame/config/DatabaseConfig.java +++ b/src/main/java/com/keuin/blame/config/DatabaseConfig.java @@ -1,11 +1,73 @@ package com.keuin.blame.config; -public record DatabaseConfig( - String address, - int port, - String database, - String table, - String username, - String password -) { +import java.util.Objects; + +@SuppressWarnings("FieldMayBeFinal") +public class DatabaseConfig { + private String address; + private Integer port; + private String database; + private String table; + private String username; + private String password; + + public DatabaseConfig(String address, Integer port, String database, String table, String username, String password) { + this.address = address; + this.port = port; + this.database = database; + this.table = table; + this.username = username; + this.password = password; + } + + public String getAddress() { + return this.address; + } + + public Integer getPort() { + return this.port; + } + + public String getDatabase() { + return this.database; + } + + public String getTable() { + return this.table; + } + + public String getUsername() { + return this.username; + } + + public String getPassword() { + return this.password; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DatabaseConfig that = (DatabaseConfig) o; + return Objects.equals(address, that.address) && Objects.equals(port, that.port) && Objects.equals(database, that.database) && Objects.equals(table, that.table) && Objects.equals(username, that.username) && Objects.equals(password, that.password); + } + + @Override + public int hashCode() { + return Objects.hash(address, port, database, table, username, password); + } + + @Override + public String toString() { + return "DatabaseConfig{" + + "address='" + address + '\'' + + ", port=" + port + + ", database='" + database + '\'' + + ", table='" + table + '\'' + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } } + + diff --git a/src/main/java/com/keuin/blame/lookup/QueryExecutor.java b/src/main/java/com/keuin/blame/lookup/QueryExecutor.java index 7acac0a..da25064 100644 --- a/src/main/java/com/keuin/blame/lookup/QueryExecutor.java +++ b/src/main/java/com/keuin/blame/lookup/QueryExecutor.java @@ -17,7 +17,7 @@ import java.util.function.Consumer; public class QueryExecutor { - private final Logger logger = LogManager.getLogger(); + private final Logger logger = LogManager.getLogger(QueryExecutor.class); private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -30,7 +30,7 @@ public class QueryExecutor { // ClickHouse driver's parameterized SQL generator is a piece of shit. // I won't use that. Use string interpolation instead. var sql = "select subject_id, object_id, action_type, ts"; - sql += " from " + escape(DatabaseUtil.DB_CONFIG.table()); + sql += " from " + escape(DatabaseUtil.DB_CONFIG.getTable()); sql += " where subject_world=%s and object_x=%d and object_y=%d and object_z=%d".formatted( escape(world), x, y, z ); diff --git a/src/main/java/com/keuin/blame/util/DatabaseUtil.java b/src/main/java/com/keuin/blame/util/DatabaseUtil.java index 1597bb5..161c71e 100644 --- a/src/main/java/com/keuin/blame/util/DatabaseUtil.java +++ b/src/main/java/com/keuin/blame/util/DatabaseUtil.java @@ -16,12 +16,12 @@ public class DatabaseUtil { public static ClickHouseNode getServer() { final var config = DatabaseUtil.DB_CONFIG; return ClickHouseNode.builder() - .host(config.address()) - .port(ClickHouseProtocol.HTTP, config.port()) + .host(config.getAddress()) + .port(ClickHouseProtocol.HTTP, config.getPort()) // .port(ClickHouseProtocol.GRPC, Integer.getInteger("chPort", 9100)) // .port(ClickHouseProtocol.TCP, Integer.getInteger("chPort", 9000)) - .database(config.database()) - .credentials(fromUserAndPassword(config.username(), config.password())) + .database(config.getDatabase()) + .credentials(fromUserAndPassword(config.getUsername(), config.getPassword())) .addOption(ClickHouseClientOption.COMPRESS.getKey(), "false") .build(); } diff --git a/src/main/java/com/keuin/blame/util/PrintUtil.java b/src/main/java/com/keuin/blame/util/PrintUtil.java index d869a8b..a2c79fb 100644 --- a/src/main/java/com/keuin/blame/util/PrintUtil.java +++ b/src/main/java/com/keuin/blame/util/PrintUtil.java @@ -31,7 +31,7 @@ public final class PrintUtil implements ServerLifecycleEvents.ServerStarted { private static final Style warnStyle = Style.EMPTY.withColor(Formatting.YELLOW); private static final Style errorStyle = Style.EMPTY.withColor(Formatting.DARK_RED); - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LogManager.getLogger(PrintUtil.class); private static final String LOG_HEADING = "[Blame]"; private static PlayerManager playerManager = null; -- cgit v1.2.3