-
Notifications
You must be signed in to change notification settings - Fork 685
Base implementation of RocksDB support #1416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c114a4
Add RocksDB backend
pipermerriam f66ba87
Ensure we dont reuse database dir for different database engines
pipermerriam 0b1a67b
Cache rocksdb installation
pipermerriam 03bfb48
PR feedback
pipermerriam 3279e60
fix bash scripts
pipermerriam 6f5b702
fix types
pipermerriam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -o errexit | ||
set -o nounset | ||
|
||
sudo apt-get install -y liblz4-dev libsnappy-dev libgflags-dev zlib1g-dev libbz2-dev libzstd-dev | ||
|
||
|
||
if [ ! -d "/home/circleci/rocksdb" ]; then | ||
git clone https://github.com/facebook/rocksdb /home/circleci/rocksdb | ||
fi | ||
if [ ! -f "/home/circleci/rocksdb/librocksdb.so.5.8.8" ]; then | ||
cd /home/circleci/rocksdb/ && git checkout v5.8.8 && sudo make install-shared INSTALL_PATH=/usr | ||
fi | ||
if [ ! -f "/usr/lib/librocksdb.so.5.8" ]; then | ||
ln -fs /home/circleci/rocksdb/librocksdb.so.5.8.8 /usr/lib/librocksdb.so.5.8 | ||
fi | ||
if [ ! -f "/usr/lib/librocksdb.so.5" ]; then | ||
ln -fs /home/circleci/rocksdb/librocksdb.so.5.8.8 /usr/lib/librocksdb.so.5 | ||
fi | ||
if [ ! -f "/usr/lib/librocksdb.so" ]; then | ||
ln -fs /home/circleci/rocksdb/librocksdb.so.5.8.8 /usr/lib/librocksdb.so | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import logging | ||
from pathlib import Path | ||
from typing import ( | ||
Generator, | ||
Iterator, | ||
TYPE_CHECKING, | ||
) | ||
|
||
|
@@ -27,10 +27,9 @@ | |
class LevelDB(BaseAtomicDB): | ||
logger = logging.getLogger("eth.db.backends.LevelDB") | ||
|
||
# Creates db as a class variable to avoid level db lock error | ||
def __init__(self, db_path: Path = None) -> None: | ||
if not db_path: | ||
raise TypeError("Please specifiy a valid path for your database.") | ||
raise TypeError("The LevelDB backend requires a database path") | ||
try: | ||
with catch_and_ignore_import_warning(): | ||
import plyvel # noqa: F811 | ||
|
@@ -54,10 +53,13 @@ def _exists(self, key: bytes) -> bool: | |
return self.db.get(key) is not None | ||
|
||
def __delitem__(self, key: bytes) -> None: | ||
v = self.db.get(key) | ||
if v is None: | ||
raise KeyError(key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without this the leveldb backend isn't fully compliant with the |
||
self.db.delete(key) | ||
|
||
@contextmanager | ||
def atomic_batch(self) -> Generator['LevelDBWriteBatch', None, None]: | ||
def atomic_batch(self) -> Iterator['LevelDBWriteBatch']: | ||
with self.db.write_batch(transaction=True) as atomic_batch: | ||
readable_batch = LevelDBWriteBatch(self, atomic_batch) | ||
try: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
from contextlib import contextmanager | ||
import logging | ||
from pathlib import Path | ||
from typing import ( | ||
Iterator, | ||
TYPE_CHECKING, | ||
) | ||
|
||
from eth_utils import ValidationError | ||
|
||
from eth.db.diff import ( | ||
DBDiffTracker, | ||
DiffMissingError, | ||
) | ||
from .base import ( | ||
BaseAtomicDB, | ||
BaseDB, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
import rocksdb # noqa: F401 | ||
|
||
|
||
class RocksDB(BaseAtomicDB): | ||
logger = logging.getLogger("eth.db.backends.RocksDB") | ||
|
||
def __init__(self, | ||
db_path: Path = None, | ||
opts: 'rocksdb.Options' = None, | ||
read_only: bool=False) -> None: | ||
if not db_path: | ||
raise TypeError("The RocksDB backend requires a database path") | ||
try: | ||
import rocksdb # noqa: F811 | ||
except ImportError: | ||
raise ImportError( | ||
"RocksDB requires the python-rocksdb library which is not " | ||
"available for import." | ||
) | ||
|
||
if opts is None: | ||
opts = rocksdb.Options(create_if_missing=True) | ||
self.db_path = db_path | ||
self.db = rocksdb.DB(str(db_path), opts, read_only=read_only) | ||
|
||
def __getitem__(self, key: bytes) -> bytes: | ||
v = self.db.get(key) | ||
if v is None: | ||
raise KeyError(key) | ||
return v | ||
|
||
def __setitem__(self, key: bytes, value: bytes) -> None: | ||
self.db.put(key, value) | ||
|
||
def _exists(self, key: bytes) -> bool: | ||
return self.db.get(key) is not None | ||
|
||
def __delitem__(self, key: bytes) -> None: | ||
exists, _ = self.db.key_may_exist(key) | ||
if not exists: | ||
raise KeyError(key) | ||
self.db.delete(key) | ||
|
||
@contextmanager | ||
def atomic_batch(self) -> Iterator['RocksDBWriteBatch']: | ||
import rocksdb # noqa: F811 | ||
batch = rocksdb.WriteBatch() | ||
|
||
readable_batch = RocksDBWriteBatch(self, batch) | ||
|
||
try: | ||
yield readable_batch | ||
finally: | ||
readable_batch.decommission() | ||
|
||
self.db.write(batch) | ||
|
||
|
||
class RocksDBWriteBatch(BaseDB): | ||
""" | ||
A native rocksdb write batch does not permit reads on the in-progress data. | ||
This class fills that gap, by tracking the in-progress diff, and adding | ||
a read interface. | ||
""" | ||
logger = logging.getLogger("eth.db.backends.RocksDBWriteBatch") | ||
|
||
def __init__(self, original_read_db: BaseDB, write_batch: 'rocksdb.WriteBatch') -> None: | ||
self._original_read_db = original_read_db | ||
self._write_batch = write_batch | ||
# keep track of the temporary changes made | ||
self._track_diff = DBDiffTracker() | ||
|
||
def __getitem__(self, key: bytes) -> bytes: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot get data from a write batch, out of context") | ||
|
||
try: | ||
changed_value = self._track_diff[key] | ||
except DiffMissingError as missing: | ||
if missing.is_deleted: | ||
raise KeyError(key) | ||
else: | ||
return self._original_read_db[key] | ||
else: | ||
return changed_value | ||
|
||
def __setitem__(self, key: bytes, value: bytes) -> None: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot set data from a write batch, out of context") | ||
|
||
self._write_batch.put(key, value) | ||
self._track_diff[key] = value | ||
|
||
def _exists(self, key: bytes) -> bool: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot test data existance from a write batch, out of context") | ||
|
||
try: | ||
self._track_diff[key] | ||
except DiffMissingError as missing: | ||
if missing.is_deleted: | ||
return False | ||
else: | ||
return key in self._original_read_db | ||
else: | ||
return True | ||
|
||
def __delitem__(self, key: bytes) -> None: | ||
if self._track_diff is None: | ||
raise ValidationError("Cannot delete data from a write batch, out of context") | ||
|
||
self._write_batch.delete(key) | ||
del self._track_diff[key] | ||
|
||
def decommission(self) -> None: | ||
""" | ||
Prevent any further actions to be taken on this write batch, called after leaving context | ||
""" | ||
self._track_diff = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this doc get stale? I suppose there's a reason you had to switch circle to use:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not stale. Requirements for rocksdb installation, will double check but I believe these are directly from their readme.
On a related note, I think our installation story is about to get ungood and we should look into debian packages and/or brew. We're gonna have to do it eventually...