Add RolesSavePerformancePatch to optimize role saving in Build 42.21. Use single SQLite transaction to reduce redundant database operations. Include hooks, tests, and integrate the patch into the performance patch set.
This commit is contained in:
1 parent
dd113961e6
commit
529e48ed5a
7 files changed
+518
-2
No files matched your search
+81
@@ -0,0 +1,81 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import zombie.network.ServerWorldDatabase;
|
||||
|
||||
/**
|
||||
* Hooks called by patched {@code zombie.characters.Roles} code: {@code Roles.save()} in one
|
||||
* SQLite transaction.
|
||||
*
|
||||
* <p>{@code Roles.save()} writes every role and capability with its own statement, each in its
|
||||
* own transaction on the autocommitting connection of {@code ServerWorldDatabase}: about 250
|
||||
* transactions and 1,000 {@code fsync}s at every shutdown. In one transaction the same
|
||||
* statements write the same rows. The hooks never fail the save: when the connection cannot be
|
||||
* reached or switched, the save runs in autocommit mode as in vanilla.
|
||||
*/
|
||||
public final class RolesHooks
|
||||
{
|
||||
/** {@code ServerWorldDatabase.conn}, package-private; {@code null} when it cannot be reached. */
|
||||
private static final @Nullable VarHandle CONNECTION;
|
||||
|
||||
static
|
||||
{
|
||||
VarHandle connection = null;
|
||||
try
|
||||
{
|
||||
connection = MethodHandles.privateLookupIn(ServerWorldDatabase.class, MethodHandles.lookup())
|
||||
.findVarHandle(ServerWorldDatabase.class, "conn", Connection.class);
|
||||
}
|
||||
catch (ReflectiveOperationException e) { System.out.println("[ZomboidForge] Roles are saved without a transaction: 'ServerWorldDatabase.conn' not found: " + e); }
|
||||
|
||||
CONNECTION = connection;
|
||||
}
|
||||
|
||||
/** The connection of the running save while it is in a transaction of these hooks. */
|
||||
private static @Nullable Connection transaction;
|
||||
|
||||
private RolesHooks() {}
|
||||
|
||||
/** Called before {@code Roles.save()}: switches the database connection to one transaction. */
|
||||
public static void beginSave()
|
||||
{
|
||||
if (RolesHooks.CONNECTION == null || ServerWorldDatabase.instance == null) return;
|
||||
|
||||
Connection connection = (Connection) RolesHooks.CONNECTION.get(ServerWorldDatabase.instance);
|
||||
if (connection == null) return;
|
||||
|
||||
try
|
||||
{
|
||||
if (!connection.getAutoCommit()) return;
|
||||
|
||||
connection.setAutoCommit(false);
|
||||
RolesHooks.transaction = connection;
|
||||
}
|
||||
catch (SQLException e) { System.out.println("[ZomboidForge] Roles are saved without a transaction: " + e); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after {@code Roles.save()}, also when it threw: commits what it wrote, as autocommit
|
||||
* would have, and switches autocommit back on.
|
||||
*/
|
||||
public static void endSave()
|
||||
{
|
||||
Connection connection = RolesHooks.transaction;
|
||||
RolesHooks.transaction = null;
|
||||
if (connection == null) return;
|
||||
|
||||
try { connection.commit(); }
|
||||
catch (SQLException e) { System.out.println("[ZomboidForge] Committing the saved roles failed: " + e); }
|
||||
finally
|
||||
{
|
||||
try { connection.setAutoCommit(true); }
|
||||
catch (SQLException e) { System.out.println("[ZomboidForge] Switching the database back to autocommit failed: " + e); }
|
||||
}
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import zombie.network.ServerWorldDatabase;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** The hooks on a real SQLite database, installed as the connection of {@code ServerWorldDatabase}. */
|
||||
class RolesHooksTest
|
||||
{
|
||||
private Connection connection;
|
||||
|
||||
private void install(Connection connection) throws ReflectiveOperationException
|
||||
{
|
||||
Field field = ServerWorldDatabase.class.getDeclaredField("conn");
|
||||
field.setAccessible(true);
|
||||
field.set(ServerWorldDatabase.instance, connection);
|
||||
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void uninstall() throws ReflectiveOperationException, SQLException
|
||||
{
|
||||
this.install(null);
|
||||
}
|
||||
|
||||
private static int count(Connection connection) throws SQLException
|
||||
{
|
||||
try (Statement statement = connection.createStatement(); ResultSet rows = statement.executeQuery("SELECT COUNT(*) FROM roles"))
|
||||
{
|
||||
rows.next();
|
||||
return rows.getInt(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void writesBetweenTheHooksBecomeVisibleTogetherAtTheEnd(@TempDir Path temp) throws Exception
|
||||
{
|
||||
String url = "jdbc:sqlite:" + temp.resolve("test.db");
|
||||
|
||||
try (Connection server = DriverManager.getConnection(url); Connection reader = DriverManager.getConnection(url))
|
||||
{
|
||||
try (Statement statement = server.createStatement()) { statement.executeUpdate("CREATE TABLE roles (name TEXT)"); }
|
||||
this.install(server);
|
||||
|
||||
RolesHooks.beginSave();
|
||||
assertFalse(server.getAutoCommit());
|
||||
|
||||
try (Statement statement = server.createStatement())
|
||||
{
|
||||
statement.executeUpdate("INSERT INTO roles VALUES ('admin')");
|
||||
statement.executeUpdate("INSERT INTO roles VALUES ('user')");
|
||||
}
|
||||
assertEquals(0, RolesHooksTest.count(reader), "Rows are visible before the end of the save.");
|
||||
|
||||
RolesHooks.endSave();
|
||||
assertTrue(server.getAutoCommit());
|
||||
assertEquals(2, RolesHooksTest.count(reader));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesAConnectionAloneThatIsAlreadyInATransaction(@TempDir Path temp) throws Exception
|
||||
{
|
||||
try (Connection server = DriverManager.getConnection("jdbc:sqlite:" + temp.resolve("test.db")))
|
||||
{
|
||||
server.setAutoCommit(false);
|
||||
this.install(server);
|
||||
|
||||
RolesHooks.beginSave();
|
||||
RolesHooks.endSave();
|
||||
|
||||
assertFalse(server.getAutoCommit());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNothingWithoutAConnection() throws ReflectiveOperationException
|
||||
{
|
||||
this.install(null);
|
||||
|
||||
RolesHooks.beginSave();
|
||||
RolesHooks.endSave();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user