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 --- build.gradle | 64 +++++++++++++----- gradle.properties | 4 +- 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 +- 7 files changed, 138 insertions(+), 37 deletions(-) diff --git a/build.gradle b/build.gradle index 6fa7bc2..574e7b8 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,12 @@ plugins { - id 'fabric-loom' version '0.8-SNAPSHOT' + id 'fabric-loom' version '1.1-SNAPSHOT' id 'maven-publish' + id 'com.github.johnrengelman.shadow' version '7.1.2' + id 'java' } -sourceCompatibility = JavaVersion.VERSION_16 -targetCompatibility = JavaVersion.VERSION_16 +sourceCompatibility = JavaVersion.VERSION_17 +targetCompatibility = JavaVersion.VERSION_17 archivesBaseName = project.archives_base_name version = project.mod_version @@ -20,6 +22,40 @@ repositories { // for more information about repositories. } +shadowJar { + configurations = [project.configurations.shadow] + + // mitigate log4j security problem + exclude 'org/apache/logging/log4j/core/lookup/JndiLookup.class' +} + +// copied from https://github.com/Siphalor/spiceoffabric/blob/1.18/build.gradle +// MIT License +// +// Copyright (c) 2021-2022 Siphalor +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +remapJar { + dependsOn(shadowJar) + inputFile = tasks.shadowJar.archiveFile +} + dependencies { implementation 'org.junit.jupiter:junit-jupiter:5.8.1' @@ -35,7 +71,9 @@ dependencies { // You may need to force-disable transitiveness on them. implementation 'junit:junit:4.13.2' modImplementation 'com.clickhouse:clickhouse-http-client:0.4.6' + shadow 'com.clickhouse:clickhouse-http-client:0.4.6' modImplementation 'com.google.code.gson:gson:2.10.1' // 2.10 supports deserializing java record objects + shadow 'com.google.code.gson:gson:2.10.1' // 2.10 supports deserializing java record objects } processResources { @@ -47,14 +85,9 @@ processResources { } tasks.withType(JavaCompile).configureEach { - // ensure that the encoding is set to UTF-8, no matter what the system default is - // this fixes some edge cases with special characters not displaying correctly - // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html - // If Javadoc is generated, this must be specified in that task too. - it.options.encoding = "UTF-8" - - // Minecraft 1.17 (21w19a) upwards uses Java 16. - it.options.release = 16 + // Minecraft 1.18 (1.18-pre2) upwards uses Java 17. + //noinspection GroovyAssignabilityCheck + it.options.release = 17 } java { @@ -73,14 +106,9 @@ jar { // configure the maven publication publishing { publications { + //noinspection GroovyAssignabilityCheck mavenJava(MavenPublication) { - // add all the jars that should be included when publishing to maven - artifact(remapJar) { - builtBy remapJar - } - artifact(sourcesJar) { - builtBy remapSourcesJar - } + from components.java } } diff --git a/gradle.properties b/gradle.properties index cd768f9..11e62d9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,9 +4,9 @@ org.gradle.jvmargs=-Xmx1G # check these on https://fabricmc.net/use minecraft_version=1.16.5 yarn_mappings=1.16.5+build.10 -loader_version=0.11.6 +loader_version=0.14.23 # Mod Properties -mod_version=1.5.1 +mod_version=2.0.0 maven_group=com.keuin.blame archives_base_name=blame-fabric # Dependencies 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