Make KeyValueStorage extend Closeable (#207)

Replace explicit usages of RocksDbKeyValueStorage with the more generic but now just as capable KeyValueStorage.
Adrian Sutton 6 years ago committed by GitHub
parent e56125c6d8
commit e5e21ba298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      ethereum/core/src/jmh/java/tech/pegasys/pantheon/ethereum/vm/operations/OperationBenchmarkHelper.java
  2. 7
      pantheon/src/main/java/tech/pegasys/pantheon/controller/CliquePantheonController.java
  3. 7
      pantheon/src/main/java/tech/pegasys/pantheon/controller/IbftPantheonController.java
  4. 7
      pantheon/src/main/java/tech/pegasys/pantheon/controller/MainnetPantheonController.java
  5. 3
      services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/InMemoryKeyValueStorage.java
  6. 3
      services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/KeyValueStorage.java
  7. 2
      services/kvstore/src/main/java/tech/pegasys/pantheon/services/kvstore/RocksDbKeyValueStorage.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.ExecutionContextTestFixture;
import tech.pegasys.pantheon.ethereum.core.MessageFrameTestFixture; import tech.pegasys.pantheon.ethereum.core.MessageFrameTestFixture;
import tech.pegasys.pantheon.ethereum.vm.MessageFrame; 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.services.kvstore.RocksDbKeyValueStorage;
import tech.pegasys.pantheon.util.uint.UInt256; import tech.pegasys.pantheon.util.uint.UInt256;
@ -34,12 +35,12 @@ import com.google.common.io.RecursiveDeleteOption;
public class OperationBenchmarkHelper { public class OperationBenchmarkHelper {
private final Path storageDirectory; private final Path storageDirectory;
private final RocksDbKeyValueStorage keyValueStorage; private final KeyValueStorage keyValueStorage;
private final MessageFrame messageFrame; private final MessageFrame messageFrame;
private OperationBenchmarkHelper( private OperationBenchmarkHelper(
final Path storageDirectory, final Path storageDirectory,
final RocksDbKeyValueStorage keyValueStorage, final KeyValueStorage keyValueStorage,
final MessageFrame messageFrame) { final MessageFrame messageFrame) {
this.storageDirectory = storageDirectory; this.storageDirectory = storageDirectory;
this.keyValueStorage = keyValueStorage; this.keyValueStorage = keyValueStorage;
@ -48,7 +49,7 @@ public class OperationBenchmarkHelper {
public static OperationBenchmarkHelper create() throws IOException { public static OperationBenchmarkHelper create() throws IOException {
final Path storageDirectory = Files.createTempDirectory("benchmark"); final Path storageDirectory = Files.createTempDirectory("benchmark");
final RocksDbKeyValueStorage keyValueStorage = RocksDbKeyValueStorage.create(storageDirectory); final KeyValueStorage keyValueStorage = RocksDbKeyValueStorage.create(storageDirectory);
final ExecutionContextTestFixture executionContext = final ExecutionContextTestFixture executionContext =
ExecutionContextTestFixture.builder().keyValueStorage(keyValueStorage).build(); ExecutionContextTestFixture.builder().keyValueStorage(keyValueStorage).build();

@ -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.api.ProtocolManager;
import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration;
import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; 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.services.kvstore.RocksDbKeyValueStorage;
import tech.pegasys.pantheon.util.time.SystemClock; import tech.pegasys.pantheon.util.time.SystemClock;
@ -108,7 +109,7 @@ public class CliquePantheonController implements PantheonController<CliqueContex
cliqueConfig.getLong("period", SECONDS_BETWEEN_BLOCKS_DEFAULT); cliqueConfig.getLong("period", SECONDS_BETWEEN_BLOCKS_DEFAULT);
final EpochManager epochManger = new EpochManager(blocksPerEpoch); final EpochManager epochManger = new EpochManager(blocksPerEpoch);
final RocksDbKeyValueStorage kv = final KeyValueStorage kv =
RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH)));
final ProtocolSchedule<CliqueContext> protocolSchedule = genesisConfig.getProtocolSchedule(); final ProtocolSchedule<CliqueContext> protocolSchedule = genesisConfig.getProtocolSchedule();
final BlockHashFunction blockHashFunction = final BlockHashFunction blockHashFunction =
@ -191,7 +192,11 @@ public class CliquePantheonController implements PantheonController<CliqueContex
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOG.error("Failed to shutdown miner executor"); LOG.error("Failed to shutdown miner executor");
} }
try {
kv.close(); kv.close();
} catch (final IOException e) {
LOG.error("Failed to close key value storage", e);
}
}); });
} }

@ -53,6 +53,7 @@ import tech.pegasys.pantheon.ethereum.p2p.api.ProtocolManager;
import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration;
import tech.pegasys.pantheon.ethereum.p2p.wire.SubProtocol; import tech.pegasys.pantheon.ethereum.p2p.wire.SubProtocol;
import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; 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.services.kvstore.RocksDbKeyValueStorage;
import java.io.IOException; import java.io.IOException;
@ -114,7 +115,7 @@ public class IbftPantheonController implements PantheonController<IbftContext> {
final int networkId, final int networkId,
final KeyPair nodeKeys) final KeyPair nodeKeys)
throws IOException { throws IOException {
final RocksDbKeyValueStorage kv = final KeyValueStorage kv =
RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH)));
final ProtocolSchedule<IbftContext> protocolSchedule = genesisConfig.getProtocolSchedule(); final ProtocolSchedule<IbftContext> protocolSchedule = genesisConfig.getProtocolSchedule();
final BlockHashFunction blockHashFunction = final BlockHashFunction blockHashFunction =
@ -186,7 +187,11 @@ public class IbftPantheonController implements PantheonController<IbftContext> {
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOG.error("Failed to shutdown ibft processor executor"); LOG.error("Failed to shutdown ibft processor executor");
} }
try {
kv.close(); kv.close();
} catch (final IOException e) {
LOG.error("Failed to close key value storage", e);
}
}; };
final TransactionPool transactionPool = final TransactionPool transactionPool =

@ -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.api.ProtocolManager;
import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration; import tech.pegasys.pantheon.ethereum.p2p.config.SubProtocolConfiguration;
import tech.pegasys.pantheon.ethereum.worldstate.KeyValueStorageWorldStateStorage; 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.services.kvstore.RocksDbKeyValueStorage;
import tech.pegasys.pantheon.util.time.SystemClock; import tech.pegasys.pantheon.util.time.SystemClock;
@ -106,7 +107,7 @@ public class MainnetPantheonController implements PantheonController<Void> {
final MiningParameters miningParams, final MiningParameters miningParams,
final KeyPair nodeKeys) final KeyPair nodeKeys)
throws IOException { throws IOException {
final RocksDbKeyValueStorage kv = final KeyValueStorage kv =
RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH))); RocksDbKeyValueStorage.create(Files.createDirectories(home.resolve(DATABASE_PATH)));
final ProtocolSchedule<Void> protocolSchedule = genesisConfig.getProtocolSchedule(); final ProtocolSchedule<Void> protocolSchedule = genesisConfig.getProtocolSchedule();
final BlockHashFunction blockHashFunction = final BlockHashFunction blockHashFunction =
@ -179,7 +180,11 @@ public class MainnetPantheonController implements PantheonController<Void> {
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOG.error("Failed to shutdown miner executor"); LOG.error("Failed to shutdown miner executor");
} }
try {
kv.close(); kv.close();
} catch (final IOException e) {
LOG.error("Failed to close key value storage", e);
}
}); });
} }

@ -85,6 +85,9 @@ public class InMemoryKeyValueStorage implements KeyValueStorage {
} }
} }
@Override
public void close() {}
private class InMemoryTransaction extends AbstractTransaction { private class InMemoryTransaction extends AbstractTransaction {
private Map<BytesValue, BytesValue> updatedValues = new HashMap<>(); private Map<BytesValue, BytesValue> updatedValues = new HashMap<>();

@ -16,12 +16,13 @@ import static com.google.common.base.Preconditions.checkState;
import tech.pegasys.pantheon.util.bytes.BytesValue; import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.io.Closeable;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
/** Service provided by pantheon to facilitate persistent data storage. */ /** Service provided by pantheon to facilitate persistent data storage. */
public interface KeyValueStorage { public interface KeyValueStorage extends Closeable {
/** /**
* @param key Index into persistent data repository. * @param key Index into persistent data repository.

@ -48,7 +48,7 @@ public class RocksDbKeyValueStorage implements KeyValueStorage, Closeable {
RocksDB.loadLibrary(); RocksDB.loadLibrary();
} }
public static RocksDbKeyValueStorage create(final Path storageDirectory) throws StorageException { public static KeyValueStorage create(final Path storageDirectory) throws StorageException {
return new RocksDbKeyValueStorage(storageDirectory); return new RocksDbKeyValueStorage(storageDirectory);
} }

Loading…
Cancel
Save