Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ static void handleUnload(Entity entity) {
unload(List.of(entity));
}

/** Saves every animal of one entities event in a single transaction instead of one per animal. */
/**
* Saves every animal of one entities event, and the entity copies taken with them, in a single
* relaxed transaction ({@link HusbandryRepository#saveLoaded}): chunks load and unload all the
* time, and each synced commit holds the server thread for a disk sync. The once-a-minute
* husbandry tick puts these saves on disk ({@link HusbandryRepository#syncRelaxed}).
*/
private static void load(Iterable<? extends Entity> entities) {
Writes writes = new Writes();
try {
Expand Down Expand Up @@ -241,7 +246,7 @@ private static void unload(Entity entity, Writes writes) {
}
}

/** Rows queued by one load or unload pass; animals are written before their snapshots, as before. */
/** Rows queued by one load or unload pass; animals are written before their snapshots. */
private static final class Writes {
private final List<HusbandryAnimal> animals = new ArrayList<>();
private final Map<UUID, byte[]> snapshots = new HashMap<>();
Expand All @@ -256,6 +261,15 @@ void flush() {
if (repository == null) {
return;
}
try {
repository.saveLoaded(animals, snapshots, System.currentTimeMillis(), true);
return;
} catch (SqliteDatabaseException together) {
// One bad row rolls back the whole pass. Save the animals and the snapshots apart,
// each fully synced as before, so only the rows that still fail are lost.
Bukkit.getLogger().warning("[Cooking] Husbandry save of a chunk pass failed as a whole, saving row by row: "
+ together.getMessage());
}
try {
repository.upsertAnimals(animals);
} catch (SqliteDatabaseException batch) {
Expand Down
184 changes: 161 additions & 23 deletions src/main/java/net/tfminecraft/cooking/husbandry/HusbandryRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.tfminecraft.cooking.husbandry;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand All @@ -13,6 +14,7 @@
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;

import net.tfminecraft.tlibs.database.SqliteDatabase;
import net.tfminecraft.tlibs.database.SqliteDatabaseException;
Expand Down Expand Up @@ -122,6 +124,13 @@ ON CONFLICT(animal_uuid) DO UPDATE SET data = excluded.data, saved_at = excluded

private final SqliteDatabase database;
private HusbandryOwnershipStore ownership;
/**
* A relaxed save has been committed and no complete checkpoint from {@link #syncRelaxed()} has
* followed it yet. Only that checkpoint clears this. A later synced commit usually puts the
* relaxed save on disk too, but not always: a commit that changes no page (the same rows
* written again) writes nothing and so syncs nothing. Read and written under the database's lock.
*/
private boolean unsynced;

private HusbandryRepository(SqliteDatabase database) {
this.database = database;
Expand Down Expand Up @@ -260,29 +269,153 @@ public void upsertAnimal(HusbandryAnimal animal) {
}

public void upsertAnimals(Collection<HusbandryAnimal> animals) {
if (animals == null || animals.isEmpty()) {
List<HusbandryAnimal> valid = valid(animals);
if (valid.isEmpty()) {
return;
}
database.runTransaction(connection -> writeAnimals(connection, valid));
}

private static List<HusbandryAnimal> valid(Collection<HusbandryAnimal> animals) {
List<HusbandryAnimal> valid = new ArrayList<>();
if (animals == null) {
return valid;
}
for (HusbandryAnimal animal : animals) {
if (animal == null || animal.uuid() == null) {
continue;
}
valid.add(animal);
}
if (valid.isEmpty()) {
return valid;
}

private static void writeAnimals(Connection connection, List<HusbandryAnimal> valid) {
try (PreparedStatement statement = connection.prepareStatement(UPSERT_ANIMAL)) {
for (HusbandryAnimal animal : valid) {
bindAnimal(statement, animal);
statement.executeUpdate();
}
} catch (SQLException e) {
throw new SqliteDatabaseException("Failed to upsert animals", e);
}
}

/**
* The routine state of loaded animals and the entity copies taken with it, in one transaction:
* the animals first, since a copy is only stored for an animal that has a row.
*
* <p>A {@code relaxed} save is committed without its own disk sync, which is what a save on the
* server thread costs. A crash or kill of the server process still loses nothing, because the
* commit already reached the operating system. {@link #syncRelaxed()} puts it on disk, and the
* once-a-minute husbandry tick calls that, so an operating-system crash or power loss can only
* roll back relaxed saves made since then. Every other write keeps {@code synchronous=FULL}.
*/
public void saveLoaded(Collection<HusbandryAnimal> animals, Map<UUID, byte[]> snapshots, long savedAt,
boolean relaxed) {
List<HusbandryAnimal> valid = valid(animals);
Map<UUID, byte[]> copies = snapshots == null ? Map.of() : snapshots;
if (valid.isEmpty() && copies.isEmpty()) {
return;
}
database.runTransaction(connection -> {
try (PreparedStatement statement = connection.prepareStatement(UPSERT_ANIMAL)) {
for (HusbandryAnimal animal : valid) {
bindAnimal(statement, animal);
statement.executeUpdate();
}
} catch (SQLException e) {
throw new SqliteDatabaseException("Failed to upsert animals", e);
Consumer<Connection> work = connection -> {
writeAnimals(connection, valid);
writeSnapshots(connection, copies, savedAt);
};
// One lock for the level change, the transaction and the change back: no other thread's
// write can run between them and be committed without its sync.
synchronized (database) {
if (!relaxed) {
// Relaxed saves before this stay marked: this commit syncs the log only if it
// changes a page, and rows written again unchanged change none.
database.runTransaction(work);
return;
}
});
Throwable failure = null;
try {
// Inside the try: a switch that fails after it took effect is undone below as well.
synchronous("NORMAL");
// Marked before the commit, not after: TLibs can still throw once the commit has
// gone through, and a spare checkpoint after a save that rolled back costs little.
unsynced = true;
database.runTransaction(work);
} catch (Throwable e) {
// Errors too: TLibs rolls back and rethrows them, and the level must still go back.
failure = e;
throw e;
} finally {
restoreSynced(failure);
}
}
}

/**
* Back to synced commits after a relaxed save, whatever became of it. A connection that cannot
* be switched back is closed: ownership and estate changes rely on their sync, so husbandry
* then fails loudly until the plugin restarts instead of committing without one.
*/
private void restoreSynced(Throwable failure) {
try {
synchronous("FULL");
} catch (Throwable restore) {
try {
closeDatabase();
} catch (Throwable closing) {
restore.addSuppressed(closing);
}
if (failure == null) {
throw new SqliteDatabaseException("Could not restore synced husbandry commits", restore);
}
// The save's own failure is the one reported; this one travels with it.
failure.addSuppressed(restore);
}
}

/** Sets how commits on this connection reach the disk. Only possible outside a transaction. */
void synchronous(String level) {
database.execute("PRAGMA synchronous=" + level);
}

/** The connection's {@code PRAGMA synchronous} level (2 = FULL, 1 = NORMAL). For tests. */
int synchronousLevel() {
return queryOne("PRAGMA synchronous", result -> result.getInt(1)).orElseThrow();
}

void closeDatabase() {
database.close();
}

/**
* Puts relaxed saves on disk, if any are waiting. They stay marked until a checkpoint has
* copied the whole log, so one that could not is tried again.
*
* @return whether no relaxed save is waiting any more
*/
public boolean syncRelaxed() {
synchronized (database) {
if (unsynced && checkpointedAll()) {
unsynced = false;
}
return !unsynced;
}
}

/**
* Runs a passive checkpoint and tells whether it copied the whole write-ahead log, which
* SQLite only does after syncing that log. A passive checkpoint does not throw when another
* connection is in its way: it reports busy, or fewer frames copied than the log holds.
*/
boolean checkpointedAll() {
// Columns: busy, frames in the log, frames checkpointed.
return queryOne("PRAGMA wal_checkpoint(PASSIVE)",
result -> result.getInt(1) == 0 && result.getInt(2) == result.getInt(3)).orElse(false);
}

/** Whether a relaxed save is still waiting for its sync. For tests. */
boolean relaxedPending() {
synchronized (database) {
return unsynced;
}
}

public Optional<HusbandryAnimal> getAnimal(UUID uuid) {
Expand Down Expand Up @@ -395,20 +528,25 @@ public void upsertSnapshots(Map<UUID, byte[]> snapshots, long savedAt) {
if (snapshots == null || snapshots.isEmpty()) {
return;
}
database.runTransaction(connection -> {
try (PreparedStatement statement = connection.prepareStatement(UPSERT_SNAPSHOT)) {
for (Map.Entry<UUID, byte[]> snapshot : snapshots.entrySet()) {
if (snapshot.getKey() == null || snapshot.getValue() == null) {
continue;
}
String uuid = snapshot.getKey().toString();
bindParams(statement, uuid, snapshot.getValue(), savedAt, uuid);
statement.executeUpdate();
database.runTransaction(connection -> writeSnapshots(connection, snapshots, savedAt));
}

private static void writeSnapshots(Connection connection, Map<UUID, byte[]> snapshots, long savedAt) {
if (snapshots.isEmpty()) {
return;
}
try (PreparedStatement statement = connection.prepareStatement(UPSERT_SNAPSHOT)) {
for (Map.Entry<UUID, byte[]> snapshot : snapshots.entrySet()) {
if (snapshot.getKey() == null || snapshot.getValue() == null) {
continue;
}
} catch (SQLException e) {
throw new SqliteDatabaseException("Failed to upsert snapshots", e);
String uuid = snapshot.getKey().toString();
bindParams(statement, uuid, snapshot.getValue(), savedAt, uuid);
statement.executeUpdate();
}
});
} catch (SQLException e) {
throw new SqliteDatabaseException("Failed to upsert snapshots", e);
}
}

public Optional<HusbandrySnapshot> getSnapshot(UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ public final class HusbandryTickTask {

private static final long PERIOD_TICKS = 20L * 60L;

/** Ticks in a row that left relaxed saves waiting for their sync before a warning is logged. */
static final int WAITING_WARN_TICKS = 5;

private static int taskId = -1;
private static int waitingTicks;

private HusbandryTickTask() {}

public static void start() {
if (taskId != -1 || Cooking.plugin == null) {
return;
}
waitingTicks = 0;
taskId = Bukkit.getScheduler().runTaskTimer(
Cooking.plugin, HusbandryTickTask::tick, PERIOD_TICKS, PERIOD_TICKS).getTaskId();
}
Expand All @@ -35,6 +40,11 @@ public static void stop() {
}
Bukkit.getScheduler().cancelTask(taskId);
taskId = -1;
// A reload starts the minute over: sync what waits now, not up to two minutes from the last tick.
HusbandryRepository repository = HusbandryEntities.repository();
if (repository != null) {
syncRelaxed(repository);
}
}

private static void tick() {
Expand Down Expand Up @@ -70,13 +80,47 @@ private static void tick() {
}
}
if (dirty.isEmpty()) {
// Nothing loaded to save: the load and unload saves since the last tick still get synced.
syncRelaxed(repository);
return;
}
try {
repository.upsertAnimals(dirty);
// One synced commit a minute for the loaded animals and their snapshots.
repository.saveLoaded(dirty, snapshots, now, false);
} catch (SqliteDatabaseException together) {
// A failing snapshot must not cost the animals their save: write the two apart.
Bukkit.getLogger().warning("[Cooking] Husbandry tick save failed as a whole, saving animals and snapshots apart: "
+ together.getMessage());
try {
repository.upsertAnimals(dirty);
} catch (SqliteDatabaseException ex) {
Bukkit.getLogger().severe("[Cooking] Failed to persist husbandry tick: " + ex.getMessage());
}
HusbandrySnapshots.save(repository, snapshots);
}
// Whatever the save above managed or changed, the relaxed load and unload saves since the
// last tick are put on disk now. A save that rewrites unchanged rows would not do that.
syncRelaxed(repository);
}

private static void syncRelaxed(HusbandryRepository repository) {
boolean synced;
try {
synced = repository.syncRelaxed();
} catch (SqliteDatabaseException ex) {
Bukkit.getLogger().severe("[Cooking] Failed to persist husbandry tick: " + ex.getMessage());
Bukkit.getLogger().severe("[Cooking] Failed to sync husbandry saves: " + ex.getMessage());
return;
}
// A checkpoint that another program holds back fails without an error, minute after minute.
if (synced) {
if (waitingTicks >= WAITING_WARN_TICKS) {
Bukkit.getLogger().info("[Cooking] Husbandry saves are synced to disk again.");
}
waitingTicks = 0;
} else if (++waitingTicks == WAITING_WARN_TICKS) {
Bukkit.getLogger().warning("[Cooking] Husbandry saves have waited " + WAITING_WARN_TICKS
+ " minutes for their disk sync, probably because another program is reading husbandry.db."
+ " A power loss would roll back the chunk load and unload saves since then.");
}
HusbandrySnapshots.save(repository, snapshots);
}
}
Loading
Loading