From 965e5f79ba7a85175c23bfe5805df111605bd705 Mon Sep 17 00:00:00 2001 From: Matt Whitehead Date: Thu, 18 May 2023 01:31:30 +0100 Subject: [PATCH] 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 * Add unit test Signed-off-by: Matthew Whitehead --------- Signed-off-by: Matthew Whitehead Co-authored-by: Matthew Whitehead --- .../RocksDBKeyValueStorageFactory.java | 5 +++- .../RocksDBKeyValueStorageFactoryTest.java | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactory.java b/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactory.java index a609596851..81aa1a3708 100644 --- a/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactory.java +++ b/plugins/rocksdb/src/main/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactory.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); } diff --git a/plugins/rocksdb/src/test/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactoryTest.java b/plugins/rocksdb/src/test/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactoryTest.java index 04b9466692..000b5079c0 100644 --- a/plugins/rocksdb/src/test/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactoryTest.java +++ b/plugins/rocksdb/src/test/java/org/hyperledger/besu/plugin/services/storage/rocksdb/RocksDBKeyValueStorageFactoryTest.java @@ -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); + } }