Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Apr 2, 2024
2 parents e107c71 + 35a49db commit 17b850b
Show file tree
Hide file tree
Showing 49 changed files with 451 additions and 173 deletions.
19 changes: 6 additions & 13 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

echo "[pre-commit check]"

STAGED_FILES=$(git --no-pager diff --name-only --staged --line-prefix=$(git rev-parse --show-toplevel)/ | paste -sd ',')

if [ ! -z "$STAGED_FILES" ]
echo -n "Checking code format with spotless: "
mvn spotless:check > /tmp/spotless.out 2>&1
RETURN_VALUE=$?
if [ $RETURN_VALUE -gt 0 ]
then
echo -n "Checking code format with spotless: "
mvn spotless:check -DspotlessFiles=$STAGED_FILES > /tmp/spotless.out 2>&1
RETURN_VALUE=$?
if [ $RETURN_VALUE -gt 0 ]
then
echo "Please run 'mvn spotless:check' for more details or 'mvn spotless:apply' to automatically fix the violations."
fi
exit $RETURN_VALUE
echo "Please run 'mvn spotless:check' for more details or 'mvn spotless:apply' to automatically fix the violations."
fi

exit 0
exit $RETURN_VALUE
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<!-- Compiler plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>

<configuration>
<excludes>
Expand Down Expand Up @@ -184,7 +184,7 @@
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.40.0</version>
<version>2.43.0</version>
<configuration>
<java>
<palantirJavaFormat>
Expand Down Expand Up @@ -313,15 +313,15 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.0</version>
<version>42.7.2</version>
<scope>compile</scope>
</dependency>

<!-- Third party plugin integrations / soft dependencies -->
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-core</artifactId>
<version>7.2.17</version>
<version>7.2.19</version>
<scope>provided</scope>

<exclusions>
Expand All @@ -335,7 +335,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.17</version>
<version>7.2.19</version>
<scope>provided</scope>

<exclusions>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/city/norain/slimefun4/EnvironmentChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.bukkit.Bukkit;

class EnvironmentChecker {
private static final List<String> UNSUPPORTED_PLUGINS =
List.of("BedrockTechnology", "SlimefunFix", "SlimefunBugFixer", "Slimefunbookfix", "MiraiMC");
private static final List<String> UNSUPPORTED_PLUGINS = List.of(
"BedrockTechnology", "SlimefunFix", "SlimefunBugFixer", "Slimefunbookfix", "PlaceItemsOnGroundRebuilt");

static boolean checkIncompatiblePlugins(@Nonnull Logger logger) {
List<String> plugins = UNSUPPORTED_PLUGINS.stream()
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/city/norain/slimefun4/SlimefunExtended.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ public static boolean checkEnvironment(@Nonnull Slimefun sf) {
return false;
}

return !EnvironmentChecker.checkIncompatiblePlugins(sf.getLogger());
if (Slimefun.getConfigManager().isBypassEnvironmentCheck()) {
sf.getLogger().log(Level.WARNING, "#######################################################");
sf.getLogger().log(Level.WARNING, "");
sf.getLogger().log(Level.WARNING, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
sf.getLogger().log(Level.WARNING, "检测到你禁用了环境兼容性检查!");
sf.getLogger().log(Level.WARNING, "未通过兼容性检查将无法受到反馈支持.");
sf.getLogger().log(Level.WARNING, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
sf.getLogger().log(Level.WARNING, "");
sf.getLogger().log(Level.WARNING, "#######################################################");
return true;
} else {
return !EnvironmentChecker.checkIncompatiblePlugins(sf.getLogger());
}
}

public static void register(@Nonnull Slimefun sf) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/city/norain/slimefun4/utils/SimpleTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package city.norain.slimefun4.utils;

import java.time.Duration;
import java.time.LocalDateTime;

public class SimpleTimer {
private final LocalDateTime startTime;
private Duration snapshot = null;

public SimpleTimer() {
startTime = LocalDateTime.now();
}

public Duration duration() {
var duration = Duration.between(startTime, LocalDateTime.now());
snapshot = duration;
return duration;
}

public String durationStr() {
if (snapshot == null) {
snapshot = duration();
}

return String.format("%d:%02d:%02d", snapshot.toHours(), snapshot.toMinutesPart(), snapshot.toSecondsPart());
}

public boolean isTimeout(Duration duration) {
if (snapshot == null) {
return false;
}

return snapshot.compareTo(duration) > 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon;

import city.norain.slimefun4.utils.SimpleTimer;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.IDataSourceAdapter;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataScope;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import com.zaxxer.hikari.HikariDataSource;
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.sql.SQLException;
import java.time.Duration;
import java.util.List;
import java.util.logging.Level;

public abstract class SqlCommonAdapter<T extends ISqlCommonConfig> implements IDataSourceAdapter<T> {
protected HikariDataSource ds;
Expand All @@ -24,25 +24,29 @@ public void prepare(T config) {
}

protected void executeSql(String sql) {
var timer = new SimpleTimer();

try (var conn = ds.getConnection()) {
SqlUtils.execSql(conn, sql);
} catch (SQLException e) {
if (Debug.hasTestCase(TestCase.DATABASE)) {
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} else {
Slimefun.logger().log(Level.WARNING, "在操作数据库出现了问题, 原始 SQL 语句: {0}", sql);
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} finally {
if (timer.isTimeout(Duration.ofSeconds(2))) { // FIXME: hardcode slow sql check duration
Debug.log(TestCase.DATABASE, "Detected slow sql costs {}, sql: {}", timer.durationStr(), sql);
}
}
}

protected List<RecordSet> executeQuery(String sql) {
var timer = new SimpleTimer();

try (var conn = ds.getConnection()) {
return SqlUtils.execQuery(conn, sql);
} catch (SQLException e) {
if (Debug.hasTestCase(TestCase.DATABASE)) {
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} else {
throw new IllegalStateException("在操作数据库出现了问题, 原始 SQL 语句: " + sql);
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} finally {
if (timer.isTimeout(Duration.ofSeconds(2))) { // FIXME: hardcode slow sql check duration
Debug.log(TestCase.DATABASE, "Detected slow sql costs {}, sql: {}", timer.durationStr(), sql);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public abstract class SqlCommonConfig implements ISqlCommonConfig {
protected final String host;
Expand Down Expand Up @@ -48,30 +46,18 @@ public HikariDataSource createDataSource() {
config.setPassword(passwd);
}

config.setMaximumPoolSize(Math.max(Runtime.getRuntime().availableProcessors(), maxConnection));
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(10));
config.setLeakDetectionThreshold(TimeUnit.MINUTES.toMillis(1));

config.setDataSourceProperties(getProperties());
config.setMaximumPoolSize(maxConnection);
config.setLeakDetectionThreshold(3000);
config.addDataSourceProperty("useLocalSessionState", "true");
config.addDataSourceProperty("rewriteBatchedStatements", "true");
config.addDataSourceProperty("cacheResultSetMetadata", "true");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("maintainTimeStats", "false");

return new HikariDataSource(config);
}

private static Properties getProperties() {
var props = new Properties();
props.setProperty("dataSource.cachePrepStmts", "true");
props.setProperty("dataSource.prepStmtCacheSize", "250");
props.setProperty("dataSource.prepStmtCacheSqlLimit", "2048");
props.setProperty("dataSource.useServerPrepStmts", "true");
props.setProperty("dataSource.useLocalSessionState", "true");
props.setProperty("dataSource.rewriteBatchedStatements", "true");
props.setProperty("dataSource.cacheResultSetMetadata", "true");
props.setProperty("dataSource.cacheServerConfiguration", "true");
props.setProperty("dataSource.elideSetAutoCommits", "true");
props.setProperty("dataSource.maintainTimeStats", "false");
return props;
}

public String tablePrefix() {
return tablePrefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.common.FieldMapper;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import io.github.bakedlibs.dough.collections.Pair;
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand Down Expand Up @@ -122,8 +120,6 @@ public static String toSqlValStr(FieldKey key, String val) {
}

public static List<RecordSet> execQuery(Connection conn, String sql) throws SQLException {
Debug.log(TestCase.DATABASE, "Prepare execute sql query: {}", sql);

try (var stmt = conn.createStatement()) {
try (var result = stmt.executeQuery(sql)) {
List<RecordSet> re = null;
Expand All @@ -148,16 +144,12 @@ public static List<RecordSet> execQuery(Connection conn, String sql) throws SQLE
}

public static void execSql(Connection conn, String sql) throws SQLException {
Debug.log(TestCase.DATABASE, "Prepare execute sql statement: {}", sql);

try (var stmt = conn.createStatement()) {
stmt.execute(sql);
}
}

public static int execUpdate(Connection conn, String sql) throws SQLException {
Debug.log(TestCase.DATABASE, "Prepare execute update statement: {}", sql);

try (var stmt = conn.createStatement()) {
return stmt.executeUpdate(sql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_RESEARCH_KEY;
import static com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlConstants.FIELD_SLIMEFUN_ID;

import city.norain.slimefun4.utils.SimpleTimer;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlCommonAdapter;
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.SqlUtils;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataScope;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.DataType;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordKey;
import com.xzavier0722.mc.plugin.slimefun4.storage.common.RecordSet;
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
import java.sql.SQLException;
import java.time.Duration;
import java.util.List;

public class SqliteAdapter extends SqlCommonAdapter<SqliteConfig> {
Expand Down Expand Up @@ -316,10 +320,15 @@ public synchronized void executeSql(String sql) {
}

private synchronized int executeUpdate(String sql) {
var timer = new SimpleTimer();
try (var conn = ds.getConnection()) {
return SqlUtils.execUpdate(conn, sql);
} catch (SQLException e) {
throw new IllegalStateException("An exception thrown while executing sql: " + sql, e);
} finally {
if (timer.isTimeout(Duration.ofSeconds(2))) { // FIXME: hardcode slow sql check duration
Debug.log(TestCase.DATABASE, "Detected slow sql costs {}, sql: {}", timer.durationStr(), sql);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.adapter.sqlcommon.ISqlCommonConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

public record SqliteConfig(String path, int maxConnection) implements ISqlCommonConfig {
public HikariDataSource createDataSource() {
Expand All @@ -13,16 +11,7 @@ public HikariDataSource createDataSource() {
config.setJdbcUrl(jdbcUrl());
config.setPoolName("SlimefunHikariPool");
config.setMaximumPoolSize(maxConnection);

config.setMaxLifetime(TimeUnit.MINUTES.toMillis(10));

var props = new Properties();
props.setProperty("dataSource.cachePrepStmts", "true");
props.setProperty("dataSource.prepStmtCacheSize", "250");
props.setProperty("dataSource.prepStmtCacheSqlLimit", "2048");
props.setProperty("dataSource.maintainTimeStats", "false");

config.setDataSourceProperties(props);
config.setLeakDetectionThreshold(3000);

return new HikariDataSource(config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.xzavier0722.mc.plugin.slimefun4.storage.common.ScopeKey;
import com.xzavier0722.mc.plugin.slimefun4.storage.task.DatabaseThreadFactory;
import com.xzavier0722.mc.plugin.slimefun4.storage.task.QueuedWriteTask;
import io.github.thebusybiscuit.slimefun4.core.debug.Debug;
import io.github.thebusybiscuit.slimefun4.core.debug.TestCase;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -76,29 +74,17 @@ public void shutdown() {
}

protected void scheduleDeleteTask(ScopeKey scopeKey, RecordKey key, boolean forceScopeKey) {
Debug.log(TestCase.DATABASE, "Scheduled remove task for key = {}", key);

scheduleWriteTask(
scopeKey,
key,
() -> {
dataAdapter.deleteData(key);
Debug.log(TestCase.DATABASE, "Data from key {} deleted.", key);
},
forceScopeKey);
}

protected void scheduleWriteTask(ScopeKey scopeKey, RecordKey key, RecordSet data, boolean forceScopeKey) {
Debug.log(TestCase.DATABASE, "Scheduled write task for key = {}, record set = {}", key, data.getAll());

scheduleWriteTask(
scopeKey,
key,
() -> {
dataAdapter.setData(key, data);
Debug.log(TestCase.DATABASE, "Data from key {} set, with record set {}", key, data.getAll());
},
forceScopeKey);
scheduleWriteTask(scopeKey, key, () -> dataAdapter.setData(key, data), forceScopeKey);
}

protected void scheduleWriteTask(ScopeKey scopeKey, RecordKey key, Runnable task, boolean forceScopeKey) {
Expand Down
Loading

0 comments on commit 17b850b

Please sign in to comment.