Skip to content

Commit

Permalink
Merge pull request bisq-network#2976 from alvasw/migration_Dont_keep_…
Browse files Browse the repository at this point in the history
…MigrationService_in_memory

migration: Don't keep MigrationService in memory
  • Loading branch information
HenrikJannsen authored Nov 5, 2024
2 parents 31b7f39 + e1b4e49 commit 57fa30b
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import bisq.common.logging.AsciiLogo;
import bisq.common.logging.LogSetup;
import bisq.common.util.ExceptionUtil;
import bisq.application.migration.MigrationService;
import bisq.i18n.Res;
import bisq.persistence.PersistenceService;
import com.typesafe.config.ConfigFactory;
Expand Down Expand Up @@ -122,6 +123,7 @@ public Config(Path baseDir,
protected final Config config;
@Getter
protected final PersistenceService persistenceService;
private Optional<MigrationService> migrationService;
private FileLock instanceLock;

public ApplicationService(String configFileName, String[] args, Path userDataDir) {
Expand Down Expand Up @@ -178,6 +180,7 @@ public ApplicationService(String configFileName, String[] args, Path userDataDir

String absoluteDataDirPath = dataDir.toAbsolutePath().toString();
persistenceService = new PersistenceService(absoluteDataDirPath);
migrationService = Optional.of(new MigrationService(dataDir));
}

private void checkInstanceLock() {
Expand All @@ -204,7 +207,11 @@ public CompletableFuture<Boolean> readAllPersisted() {
return persistenceService.readAllPersisted();
}

public abstract CompletableFuture<Boolean> initialize();
public CompletableFuture<Boolean> initialize() {
CompletableFuture<Boolean> completableFuture = migrationService.orElseThrow().initialize();
migrationService = Optional.empty();
return completableFuture;
}

public abstract CompletableFuture<Boolean> shutdown();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bisq.evolution.migration;
package bisq.application.migration;

import bisq.application.migration.migrations.Migration;
import bisq.application.migration.migrations.MigrationsForV2_1_2;
import bisq.common.application.ApplicationVersion;
import bisq.common.application.Service;
import bisq.common.platform.Version;
Expand All @@ -8,6 +10,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class MigrationService implements Service {
Expand All @@ -26,7 +29,8 @@ public CompletableFuture<Boolean> initialize() {
Version appVersion = ApplicationVersion.getVersion();

if (dataDirVersion.below(appVersion)) {
Migrator migrator = new Migrator(appVersion, dataDir);
List<Migration> allMigrations = List.of(new MigrationsForV2_1_2());
Migrator migrator = new Migrator(appVersion, dataDir, allMigrations);
migrator.migrate();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package bisq.evolution.migration;
package bisq.application.migration;

import bisq.common.application.ApplicationVersion;
import bisq.common.platform.Version;
import bisq.evolution.migration.migrations.Migration;
import bisq.evolution.migration.migrations.MigrationFailedException;
import bisq.evolution.migration.migrations.MigrationsForV2_1_2;
import bisq.application.migration.migrations.Migration;
import bisq.application.migration.migrations.MigrationFailedException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
Expand All @@ -18,10 +17,10 @@ public class Migrator {
private final Path dataDir;
private final List<Migration> allMigrations;

public Migrator(Version appVersion, Path dataDir) {
public Migrator(Version appVersion, Path dataDir, List<Migration> allMigrations) {
this.appVersion = appVersion;
this.dataDir = dataDir;
this.allMigrations = List.of(new MigrationsForV2_1_2());
this.allMigrations = allMigrations;
}

public void migrate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bisq.evolution.migration.migrations;
package bisq.application.migration.migrations;

import bisq.common.platform.Version;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package bisq.evolution.migration.migrations;
package bisq.application.migration.migrations;

public class MigrationFailedException extends RuntimeException {
public MigrationFailedException(String message) {
super(message);
}

public MigrationFailedException(Throwable cause) {
super(cause);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bisq.evolution.migration.migrations;
package bisq.application.migration.migrations;

import bisq.common.file.FileUtils;
import bisq.common.platform.OS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bisq.evolution.migration;
package bisq.application.migration;

import bisq.application.migration.MigrationService;
import bisq.common.platform.InvalidVersionException;
import bisq.common.platform.Version;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package bisq.application.migration;

import bisq.application.migration.migrations.Migration;
import bisq.application.migration.migrations.MigrationFailedException;
import bisq.common.application.ApplicationVersion;
import bisq.common.platform.Version;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class MigratorTest {
@Test
void migrationSuccess(@TempDir Path dataDir) throws IOException {
Path versionFilePath = dataDir.resolve("version");
Version dataDirVersion = new Version("2.1.0");
Files.writeString(versionFilePath, dataDirVersion.toString());

Version appVersion = ApplicationVersion.getVersion();
Migrator migrator = new Migrator(appVersion, dataDir, Collections.emptyList());

migrator.migrate();

String readVersion = Files.readString(dataDir.resolve("version"));
assertThat(readVersion).isEqualTo(appVersion.toString());
}

@Test
void migrationFailure(@TempDir Path dataDir) throws IOException {
Path versionFilePath = dataDir.resolve("version");
Version dataDirVersion = new Version("2.1.0");
Files.writeString(versionFilePath, dataDirVersion.toString());

Version appVersion = ApplicationVersion.getVersion();
var migration = new Migration() {
@Override
public void run(Path dataDir) {
throw new MigrationFailedException("Migration failed.");
}

@Override
public Version getVersion() {
return new Version("2.1.1");
}
};

Migrator migrator = new Migrator(appVersion, dataDir, List.of(migration, migration));
migrator.migrate();

String readVersion = Files.readString(dataDir.resolve("version"));
assertThat(readVersion).isEqualTo(dataDirVersion.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bisq.evolution.migration.migrations;
package bisq.application.migration.migrations;

import bisq.application.migration.migrations.MigrationsForV2_1_2;
import bisq.common.platform.Version;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ private Optional<OsSpecificNotificationService> findSystemNotificationDelegate()

@Override
public CompletableFuture<Boolean> initialize() {
return migrationService.initialize()
.thenCompose(result -> memoryReportService.initialize())
return memoryReportService.initialize()
.thenCompose(result -> securityService.initialize())
.thenCompose(result -> {
setState(State.INITIALIZE_NETWORK);
Expand Down Expand Up @@ -338,7 +337,6 @@ public CompletableFuture<Boolean> shutdown() {
.orElse(CompletableFuture.completedFuture(true)))
.thenCompose(result -> securityService.shutdown().exceptionally(this::logError))
.thenCompose(result -> memoryReportService.shutdown().exceptionally(this::logError))
.thenCompose(result -> migrationService.shutdown().exceptionally(this::logError))
.orTimeout(SHUTDOWN_TIMEOUT_SEC, TimeUnit.SECONDS)
.handle((result, throwable) -> {
if (throwable == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public OracleNodeApplicationService(String[] args) {

@Override
public CompletableFuture<Boolean> initialize() {
return migrationService.initialize()
.thenCompose(result -> memoryReportService.initialize())
return memoryReportService.initialize()
.thenCompose(result -> securityService.initialize())
.thenCompose(result -> networkService.initialize())
.thenCompose(result -> identityService.initialize())
Expand All @@ -110,7 +109,6 @@ public CompletableFuture<Boolean> shutdown() {
.thenCompose(result -> networkService.shutdown())
.thenCompose(result -> securityService.shutdown())
.thenCompose(result -> memoryReportService.shutdown())
.thenCompose(result -> migrationService.shutdown())
.orTimeout(2, TimeUnit.MINUTES)
.handle((result, throwable) -> throwable == null)
.join());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ public RestApiApplicationService(String[] args) {

@Override
public CompletableFuture<Boolean> initialize() {
return migrationService.initialize()
.thenCompose(result -> memoryReportService.initialize())
return memoryReportService.initialize()
.thenCompose(result -> securityService.initialize())
.thenCompose(result -> {
setState(State.INITIALIZE_NETWORK);
Expand Down Expand Up @@ -255,7 +254,6 @@ public CompletableFuture<Boolean> shutdown() {
.orElse(CompletableFuture.completedFuture(true)))
.thenCompose(result -> securityService.shutdown())
.thenCompose(result -> memoryReportService.shutdown())
.thenCompose(result -> migrationService.shutdown())
.orTimeout(10, TimeUnit.SECONDS)
.handle((result, throwable) -> throwable == null)
.join());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public SeedNodeApplicationService(String[] args) {

@Override
public CompletableFuture<Boolean> initialize() {
return migrationService.initialize()
.thenCompose(result -> memoryReportService.initialize())
return memoryReportService.initialize()
.thenCompose(result -> securityService.initialize())
.thenCompose(result -> networkService.initialize())
.thenCompose(result -> identityService.initialize())
Expand Down Expand Up @@ -105,7 +104,6 @@ public CompletableFuture<Boolean> shutdown() {
.thenCompose(result -> networkService.shutdown())
.thenCompose(result -> securityService.shutdown())
.thenCompose(result -> memoryReportService.shutdown())
.thenCompose(result -> migrationService.shutdown())
.orTimeout(10, TimeUnit.SECONDS)
.handle((result, throwable) -> {
if (throwable != null) {
Expand Down
29 changes: 0 additions & 29 deletions evolution/src/test/java/bisq/evolution/migration/MigratorTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import bisq.common.facades.FacadeProvider;
import bisq.common.platform.MemoryReportService;
import bisq.common.platform.PlatformUtils;
import bisq.evolution.migration.MigrationService;
import bisq.java_se.facades.JavaSeGuavaFacade;
import bisq.java_se.facades.JavaSeJdkFacade;
import bisq.java_se.jvm.JvmMemoryReportService;
Expand All @@ -31,7 +30,6 @@
@Getter
@Slf4j
public abstract class JavaSeApplicationService extends ApplicationService {
protected final MigrationService migrationService;
protected final MemoryReportService memoryReportService;

public JavaSeApplicationService(String configFileName, String[] args) {
Expand All @@ -43,7 +41,6 @@ public JavaSeApplicationService(String configFileName, String[] args) {
FacadeProvider.setGuavaFacade(new JavaSeGuavaFacade());
FacadeProvider.setJdkFacade(new JavaSeJdkFacade());

migrationService = new MigrationService(getConfig().getBaseDir());
memoryReportService = new JvmMemoryReportService(getConfig().getMemoryReportIntervalSec(), getConfig().isIncludeThreadListInMemoryReport());
}
}

0 comments on commit 57fa30b

Please sign in to comment.