From e5e21ba29814536cd8bd11879fda26176a3c9300 Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Tue, 30 Oct 2018 06:38:48 +1000 Subject: [PATCH] Make KeyValueStorage extend Closeable (#207) Replace explicit usages of RocksDbKeyValueStorage with the more generic but now just as capable KeyValueStorage. --- .../ethereum/vm/operations/OperationBenchmarkHelper.java | 7 ++++--- .../pantheon/controller/CliquePantheonController.java | 9 +++++++-- .../pantheon/controller/IbftPantheonController.java | 9 +++++++-- .../pantheon/controller/MainnetPantheonController.java | 9 +++++++-- .../services/kvstore/InMemoryKeyValueStorage.java | 3 +++ .../pantheon/services/kvstore/KeyValueStorage.java | 3 ++- .../services/kvstore/RocksDbKeyValueStorage.java | 2 +- 7 files changed, 31 insertions(+), 11 deletions(-) diff --git a/ethereum/core/src/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/OperationBenchmarkHelper.java b/ethereum/core/src/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/OperationBenchmarkHelper.java index a2bace71d8..6537e1ff2e 100644 --- a/ethereum/core/src/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/OperationBenchmarkHelper.java +++ b/ethereum/core/src/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/OperationBenchmarkHelper.java @@ -21,6 +21,7 @@ import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; import tech.pegasys.pantheon.ethereum.core.ExecutionContextTestFixture; import tech.pegasys.pantheon.ethereum.core.MessageFrameTestFixture; import tech.pegasys.pantheon.ethereum.vm.MessageFrame; +import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; import tech.pegasys.pantheon.util.uint.UInt256; @@ -34,12 +35,12 @@ import com.google.common.io.RecursiveDeleteOption; public class OperationBenchmarkHelper { private final Path storageDirectory; - private final RocksDbKeyValueStorage keyValueStorage; + private final KeyValueStorage keyValueStorage; private final MessageFrame messageFrame; private OperationBenchmarkHelper( final Path storageDirectory, - final RocksDbKeyValueStorage keyValueStorage, + final KeyValueStorage keyValueStorage, final MessageFrame messageFrame) { this.storageDirectory = storageDirectory; this.keyValueStorage = keyValueStorage; @@ -48,7 +49,7 @@ public class OperationBenchmarkHelper { public static OperationBenchmarkHelper create() throws IOException { final Path storageDirectory = Files.createTempDirectory("benchmark"); - final RocksDbKeyValueStorage keyValueStorage = RocksDbKeyValueStorage.create(storageDirectory); + final KeyValueStorage keyValueStorage = RocksDbKeyValueStorage.create(storageDirectory); final ExecutionContextTestFixture executionContext = ExecutionContextTestFixture.builder().keyValueStorage(keyValueStorage).build(); diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java index f300bf21eb..6527932560 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java @@ -47,6 +47,7 @@ import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; +import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; import tech.pegasys.pantheon.util.time.SystemClock; @@ -108,7 +109,7 @@ public class CliquePantheonController implements PantheonController protocolSchedule = genesisConfig.getProtocolSchedule(); final BlockHashFunction blockHashFunction = @@ -191,7 +192,11 @@ public class CliquePantheonController implements PantheonController { final int networkId, final KeyPair nodeKeys) throws IOException { - final RocksDbKeyValueStorage kv = + final KeyValueStorage kv = RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); final ProtocolSchedule protocolSchedule = genesisConfig.getProtocolSchedule(); final BlockHashFunction blockHashFunction = @@ -186,7 +187,11 @@ public class IbftPantheonController implements PantheonController { } catch (final InterruptedException e) { LOG.error("Failed to shutdown ibft processor executor"); } - kv.close(); + try { + kv.close(); + } catch (final IOException e) { + LOG.error("Failed to close key value storage", e); + } }; final TransactionPool transactionPool = diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java b/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java index da3940056f..244e0b611b 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java @@ -42,6 +42,7 @@ import tech.pegasys.pantheon.ethereum.mainnet.ScheduleBasedBlockHashFunction; import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; +import tech.pegasys.pantheon.services.kvstore.KeyValueStorage; import tech.pegasys.pantheon.services.kvstore.RocksDbKeyValueStorage; import tech.pegasys.pantheon.util.time.SystemClock; @@ -106,7 +107,7 @@ public class MainnetPantheonController implements PantheonController { final MiningParameters miningParams, final KeyPair nodeKeys) throws IOException { - final RocksDbKeyValueStorage kv = + final KeyValueStorage kv = RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); final ProtocolSchedule protocolSchedule = genesisConfig.getProtocolSchedule(); final BlockHashFunction blockHashFunction = @@ -179,7 +180,11 @@ public class MainnetPantheonController implements PantheonController { } catch (final InterruptedException e) { LOG.error("Failed to shutdown miner executor"); } - kv.close(); + try { + kv.close(); + } catch (final IOException e) { + LOG.error("Failed to close key value storage", e); + } }); } diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java index 717022e72d..a8469fa3e8 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java @@ -85,6 +85,9 @@ public class InMemoryKeyValueStorage implements KeyValueStorage { } } + @Override + public void close() {} + private class InMemoryTransaction extends AbstractTransaction { private Map updatedValues = new HashMap<>(); diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java index 99440c7bd7..20ab2d134b 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java @@ -16,12 +16,13 @@ import static com.google.common.base.Preconditions.checkState; import tech.pegasys.pantheon.util.bytes.BytesValue; +import java.io.Closeable; import java.util.Objects; import java.util.Optional; import java.util.stream.Stream; /** Service provided by pantheon to facilitate persistent data storage. */ -public interface KeyValueStorage { +public interface KeyValueStorage extends Closeable { /** * @param key Index into persistent data repository. diff --git a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java index 59361600e5..036484d0cd 100644 --- a/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java +++ b/services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.java @@ -48,7 +48,7 @@ public class RocksDbKeyValueStorage implements KeyValueStorage, Closeable { RocksDB.loadLibrary(); } - public static RocksDbKeyValueStorage create(final Path storageDirectory) throws StorageException { + public static KeyValueStorage create(final Path storageDirectory) throws StorageException { return new RocksDbKeyValueStorage(storageDirectory); }