Only create DB data dir if it doesn't already exist (#5453)

* Only create DB data dir if it doesn't already exist

Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>

* Add unit test

Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>

---------

Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
Co-authored-by: Matthew Whitehead <matthew1001@gmail.com>
pull/5479/head
Matt Whitehead 2 years ago committed by GitHub
parent 74a9017639
commit 965e5f79ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactory.java
  2. 23
      plugins/rocksdb/src/test/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactoryTest.java

@ -255,6 +255,7 @@ public class RocksDBKeyValueStorageFactory implements KeyValueStorageFactory {
private int readDatabaseVersion(final BesuConfiguration commonConfiguration) throws IOException {
final Path dataDir = commonConfiguration.getDataPath();
final boolean databaseExists = commonConfiguration.getStoragePath().toFile().exists();
final boolean dataDirExists = dataDir.toFile().exists();
final int databaseVersion;
if (databaseExists) {
databaseVersion = DatabaseMetadata.lookUpFrom(dataDir).getVersion();
@ -265,7 +266,9 @@ public class RocksDBKeyValueStorageFactory implements KeyValueStorageFactory {
} else {
databaseVersion = commonConfiguration.getDatabaseVersion();
LOG.info("No existing database detected at {}. Using version {}", dataDir, databaseVersion);
Files.createDirectories(dataDir);
if (!dataDirExists) {
Files.createDirectories(dataDir);
}
new DatabaseMetadata(databaseVersion).writeToDirectory(dataDir);
}

@ -199,4 +199,27 @@ public class RocksDBKeyValueStorageFactoryTest {
.create(segment, commonConfiguration, metricsSystem))
.isInstanceOf(IllegalStateException.class);
}
@Test
public void shouldCreateDBCorrectlyIfSymlink() throws Exception {
final Path tempRealDataDir =
Files.createDirectories(temporaryFolder.newFolder().toPath().resolve("real-data-dir"));
final Path tempSymLinkDataDir =
Files.createSymbolicLink(
temporaryFolder.newFolder().toPath().resolve("symlink-data-dir"), tempRealDataDir);
final Path tempDatabaseDir = temporaryFolder.newFolder().toPath().resolve("db");
when(commonConfiguration.getStoragePath()).thenReturn(tempDatabaseDir);
when(commonConfiguration.getDataPath()).thenReturn(tempSymLinkDataDir);
when(commonConfiguration.getDatabaseVersion()).thenReturn(DEFAULT_VERSION);
final RocksDBKeyValueStorageFactory storageFactory =
new RocksDBKeyValueStorageFactory(
() -> rocksDbConfiguration, segments, RocksDBMetricsFactory.PUBLIC_ROCKS_DB_METRICS);
// Ensure that having created everything via a symlink data dir the DB meta-data has been
// created correctly
storageFactory.create(segment, commonConfiguration, metricsSystem);
assertThat(DatabaseMetadata.lookUpFrom(tempRealDataDir).getVersion())
.isEqualTo(DEFAULT_VERSION);
}
}

Loading…
Cancel
Save