Skip to content

Commit b630e05

Browse files
committed
Merge branch '1.21.8' into 1.21.10
2 parents abc39ba + 66c7414 commit b630e05

6 files changed

Lines changed: 53 additions & 27 deletions

File tree

common/src/main/java/xaeroplus/feature/db/DatabaseMigration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import java.util.concurrent.CompletionException;
1010

1111
public interface DatabaseMigration {
12-
boolean shouldMigrate(String databaseName, Connection connection) throws SQLException;
13-
void doMigration(String databaseName, Connection connection) throws SQLException, InterruptedException;
12+
boolean shouldMigrate(String databaseName, Connection connection, boolean init) throws SQLException;
13+
void doMigration(String databaseName, Connection connection, boolean init) throws SQLException, InterruptedException;
1414

1515
static boolean isCorruptDatabase(final Throwable throwable) {
1616
var current = throwable;

common/src/main/java/xaeroplus/feature/db/DatabaseMigrator.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,21 @@ public Connection migrate(Path dbPath, String databaseName, Connection connectio
5353
NotificationUtil.inGameNotification("Database: " + databaseName + " recovered successfully! Retrying migration...");
5454
continue;
5555
}
56-
XaeroPlus.LOGGER.error("Failed migrating database: {}", databaseName, e);
57-
NotificationUtil.inGameNotification("Database: " + databaseName + " failed to migrate!");
58-
NotificationUtil.inGameNotification("More info will be in your log");
56+
if (e instanceof InterruptedException) {
57+
XaeroPlus.LOGGER.warn("Migration interrupted: {}", databaseName);
58+
} else {
59+
XaeroPlus.LOGGER.error("Failed migrating database: {}", databaseName, e);
60+
NotificationUtil.inGameNotification("Database: " + databaseName + " failed to migrate!");
61+
NotificationUtil.inGameNotification("More info will be in your log");
62+
}
5963
throw e;
6064
}
6165
}
6266
}
6367

6468
private Connection migrateExisting(final Path dbPath, String databaseName, Connection connection) throws SQLException, InterruptedException {
6569
for (var migration : migrations) {
66-
if (migration.shouldMigrate(databaseName, connection)) {
70+
if (migration.shouldMigrate(databaseName, connection, false)) {
6771
long beforeMigration = System.nanoTime();
6872
XaeroPlus.LOGGER.info("Found database: {} that needs migration", databaseName);
6973
MIGRATION_MONITOR.onMigrationStart(databaseName);
@@ -72,18 +76,18 @@ private Connection migrateExisting(final Path dbPath, String databaseName, Conne
7276
long beforeBackup = System.nanoTime();
7377
var backupPath = backupDatabase(dbPath, databaseName, connection);
7478
long afterBackup = System.nanoTime();
75-
XaeroPlus.LOGGER.info("Backed up database: {} to {} in {} ms", databaseName, backupPath, (afterBackup - beforeBackup) / 1000000);
79+
XaeroPlus.LOGGER.info("Backed up database: {} to {} in {} ms", databaseName, backupPath, TimeUnit.NANOSECONDS.toMillis(afterBackup - beforeBackup));
7680
long beforeRunMigration = System.nanoTime();
77-
runMigration(databaseName, connection, migration);
81+
runMigration(databaseName, connection, migration, false);
7882
long afterRunMigration = System.nanoTime();
79-
XaeroPlus.LOGGER.info("Ran migration: {} in {} ms", migration.getClass().getSimpleName(), (afterRunMigration - beforeRunMigration) / 1000000);
83+
XaeroPlus.LOGGER.info("Ran migration: {} in {} ms", migration.getClass().getSimpleName(), TimeUnit.NANOSECONDS.toMillis(afterRunMigration - beforeRunMigration));
8084
long beforeVacuum = System.nanoTime();
8185
vacuum(connection);
8286
long afterVacuum = System.nanoTime();
83-
XaeroPlus.LOGGER.info("Vacuumed database: {} in {} ms", databaseName, (afterVacuum - beforeVacuum) / 1000000);
87+
XaeroPlus.LOGGER.info("Vacuumed database: {} in {} ms", databaseName, TimeUnit.NANOSECONDS.toMillis(afterVacuum - beforeVacuum));
8488
});
8589
long afterMigration = System.nanoTime();
86-
XaeroPlus.LOGGER.info("completed {} migration duration in {} ms", databaseName, (afterMigration - beforeMigration) / 1000000);
90+
XaeroPlus.LOGGER.info("completed {} migration duration in {} ms", databaseName, TimeUnit.NANOSECONDS.toMillis(afterMigration - beforeMigration));
8791
MIGRATION_MONITOR.onMigrationEnd(databaseName, true);
8892
}
8993
}
@@ -92,8 +96,8 @@ private Connection migrateExisting(final Path dbPath, String databaseName, Conne
9296

9397
private Connection migrateInit(final Path dbPath, String databaseName, Connection connection) throws SQLException, InterruptedException {
9498
for (var migration : migrations) {
95-
if (migration.shouldMigrate(databaseName, connection)) {
96-
runMigration(databaseName, connection, migration);
99+
if (migration.shouldMigrate(databaseName, connection, true)) {
100+
runMigration(databaseName, connection, migration, true);
97101
}
98102
}
99103
return connection;
@@ -102,16 +106,20 @@ private Connection migrateInit(final Path dbPath, String databaseName, Connectio
102106
private void runMigration(
103107
final String databaseName,
104108
final Connection connection,
105-
final DatabaseMigration migration
109+
final DatabaseMigration migration,
110+
final boolean init
106111
) throws SQLException, InterruptedException {
107112
var committed = false;
108113
try {
109114
connection.setAutoCommit(false);
110-
migration.doMigration(databaseName, connection);
115+
migration.doMigration(databaseName, connection, init);
111116
if (Thread.currentThread().isInterrupted()) {
112117
throw new InterruptedException("Migration interrupted");
113118
}
119+
long beforeCommit = System.nanoTime();
114120
connection.commit();
121+
long afterCommit = System.nanoTime();
122+
if (!init) XaeroPlus.LOGGER.info("Committed migration: {} in {} ms", migration.getClass().getSimpleName(), TimeUnit.NANOSECONDS.toMillis(afterCommit - beforeCommit));
115123
committed = true;
116124
} finally {
117125
try {
@@ -132,7 +140,7 @@ private void executeConcurrencyLimited(
132140
final String operation,
133141
final SqlOperation sqlOperation
134142
) throws SQLException, InterruptedException {
135-
var permitAcquired = HEAVY_OPERATION_PERMITS.tryAcquire(48, TimeUnit.HOURS);
143+
var permitAcquired = HEAVY_OPERATION_PERMITS.tryAcquire(100, TimeUnit.HOURS);
136144
if (!permitAcquired) {
137145
throw new RuntimeException("Failed to acquire permit for database " + operation + ": " + databaseName);
138146
}
@@ -231,9 +239,10 @@ private void validateAvailableDiskSpace(Path dbPath) {
231239
if (!dbPath.toFile().exists()) return;
232240
try {
233241
var dbSize = Files.size(dbPath);
242+
XaeroPlus.LOGGER.info("Database size: {} mb", dbSize / (1024 * 1024));
234243
long freeSpace = dbPath.getParent().toFile().getUsableSpace();
235244
if (freeSpace < dbSize * 3) {
236-
throw new RuntimeException("Not enough available disk space to migrate database: " + dbPath.toFile().getName());
245+
throw new RuntimeException("Not enough available disk space to migrate database: " + dbPath.toFile().getName() + " - " + freeSpace / (1024 * 1024) + "mb available");
237246
}
238247
} catch (IOException e) {
239248
throw new RuntimeException("Failed to check available disk space for database: " + dbPath.toFile().getName(), e);

common/src/main/java/xaeroplus/feature/drawing/db/V0Migration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class V0Migration implements DatabaseMigration {
1313
@Override
14-
public boolean shouldMigrate(final String databaseName, final Connection connection) {
14+
public boolean shouldMigrate(final String databaseName, final Connection connection, final boolean init) {
1515
try {
1616
if (!tableExists("metadata", connection)) return true;
1717
try (var statement = connection.createStatement()) {
@@ -30,7 +30,7 @@ public boolean shouldMigrate(final String databaseName, final Connection connect
3030
}
3131

3232
@Override
33-
public void doMigration(final String databaseName, final Connection connection) {
33+
public void doMigration(final String databaseName, final Connection connection, final boolean init) {
3434
createMetadataTable(databaseName, connection);
3535
createLinesTable(databaseName, connection, Level.OVERWORLD);
3636
createLinesTable(databaseName, connection, Level.NETHER);

common/src/main/java/xaeroplus/feature/highlights/ChunkHighlightSavingCache.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,11 @@ public void onSuccess(@Nullable final Object result) {
179179

180180
@Override
181181
public void onFailure(@NotNull final Throwable t) {
182-
XaeroPlus.LOGGER.error("Error initializing {} disk cache", databaseName, t);
182+
if (t instanceof CancellationException) {
183+
XaeroPlus.LOGGER.warn("{} disk cache initialization cancelled", databaseName);
184+
} else {
185+
XaeroPlus.LOGGER.error("Error initializing {} disk cache", databaseName, t);
186+
}
183187
cacheReady.set(false);
184188
reset();
185189
}

common/src/main/java/xaeroplus/feature/highlights/db/V0ToV1Migration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class V0ToV1Migration implements DatabaseMigration {
1818
*/
1919

2020
@Override
21-
public boolean shouldMigrate(String databaseName, Connection connection) {
21+
public boolean shouldMigrate(String databaseName, Connection connection, boolean init) {
2222
try {
2323
return tableExists("0", connection)
2424
|| tableExists("-1", connection)
@@ -33,7 +33,7 @@ public boolean shouldMigrate(String databaseName, Connection connection) {
3333
}
3434

3535
@Override
36-
public void doMigration(String databaseName, final Connection connection) {
36+
public void doMigration(String databaseName, final Connection connection, final boolean init) {
3737
// migrate old tables and indexes
3838
try {
3939
// migrate table name

common/src/main/java/xaeroplus/feature/highlights/db/V1ToV2Migration.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import java.util.ArrayList;
1010
import java.util.List;
1111
import java.util.Locale;
12+
import java.util.concurrent.TimeUnit;
1213

1314
import static xaeroplus.feature.db.DatabaseMigration.executeCancellable;
1415

1516
public class V1ToV2Migration implements DatabaseMigration {
1617
private static final int VERSION = 2;
1718

1819
@Override
19-
public boolean shouldMigrate(final String databaseName, final Connection connection) throws SQLException {
20+
public boolean shouldMigrate(final String databaseName, final Connection connection, final boolean init) throws SQLException {
2021
try {
2122
return getMetadataVersion(connection) < VERSION;
2223
} catch (SQLException ex) {
@@ -27,10 +28,10 @@ public boolean shouldMigrate(final String databaseName, final Connection connect
2728
}
2829

2930
@Override
30-
public void doMigration(final String databaseName, final Connection connection) throws SQLException, InterruptedException {
31+
public void doMigration(final String databaseName, final Connection connection, final boolean init) throws SQLException, InterruptedException {
3132
for (var tableName : getHighlightTableNames(connection)) {
3233
if (isWithoutRowid(tableName, connection)) continue;
33-
migrateTable(databaseName, connection, tableName);
34+
migrateTable(databaseName, connection, tableName, init);
3435
}
3536
setMetadataVersion(connection);
3637
}
@@ -72,16 +73,28 @@ private boolean isHighlightTable(final String tableName, final Connection connec
7273
return hasX && hasZ && hasFoundTime;
7374
}
7475

75-
private void migrateTable(final String databaseName, final Connection connection, final String tableName) throws SQLException, InterruptedException {
76+
private void migrateTable(final String databaseName, final Connection connection, final String tableName, final boolean init) throws SQLException, InterruptedException {
7677
var newTableName = tableName + "_v2_migration";
77-
XaeroPlus.LOGGER.info("Migrating {} database table {} to V2", databaseName, tableName);
78+
if (!init) XaeroPlus.LOGGER.info("Migrating {} database table {} to V2", databaseName, tableName);
79+
long before = System.nanoTime();
7880
executeCancellable(connection, "DROP TABLE IF EXISTS " + quoteIdentifier(newTableName));
81+
long step1 = System.nanoTime();
82+
if (!init) XaeroPlus.LOGGER.info("migration step 1/5 {} ms", TimeUnit.NANOSECONDS.toMillis(step1 - before));
7983
executeCancellable(connection, "CREATE TABLE " + quoteIdentifier(newTableName)
8084
+ " (x INTEGER, z INTEGER, foundTime INTEGER, PRIMARY KEY (x, z)) WITHOUT ROWID");
85+
long step2 = System.nanoTime();
86+
if (!init) XaeroPlus.LOGGER.info("migration step 2/5 {} ms", TimeUnit.NANOSECONDS.toMillis(step2 - step1));
8187
executeCancellable(connection, "INSERT INTO " + quoteIdentifier(newTableName)
8288
+ " (x, z, foundTime) SELECT x, z, foundTime FROM " + quoteIdentifier(tableName));
89+
long step3 = System.nanoTime();
90+
if (!init) XaeroPlus.LOGGER.info("migration step 3/5 {} ms", TimeUnit.NANOSECONDS.toMillis(step3 - step2));
8391
executeCancellable(connection, "DROP TABLE " + quoteIdentifier(tableName));
92+
long step4 = System.nanoTime();
93+
if (!init) XaeroPlus.LOGGER.info("migration step 4/5 {} ms", TimeUnit.NANOSECONDS.toMillis(step4 - step3));
8494
executeCancellable(connection, "ALTER TABLE " + quoteIdentifier(newTableName) + " RENAME TO " + quoteIdentifier(tableName));
95+
long done = System.nanoTime();
96+
if (!init) XaeroPlus.LOGGER.info("migration step 5/5 {} ms", TimeUnit.NANOSECONDS.toMillis(done - step4));
97+
if (!init) XaeroPlus.LOGGER.info("{} migration total {} ms", tableName, TimeUnit.NANOSECONDS.toMillis(done - before));
8598
}
8699

87100
private int getMetadataVersion(final Connection connection) throws SQLException {

0 commit comments

Comments
 (0)