diff --git a/store/v2/commitment/store_bench_test.go b/store/v2/commitment/store_bench_test.go index c8343d942b72..f04ec4d0717b 100644 --- a/store/v2/commitment/store_bench_test.go +++ b/store/v2/commitment/store_bench_test.go @@ -1,6 +1,3 @@ -//go:build rocksdb -// +build rocksdb - package commitment_test import ( @@ -20,9 +17,6 @@ import ( var ( storeKeys = []string{"store1", "store2", "store3"} dbBackends = map[string]func(dataDir string) (corestore.KVStoreWithBatch, error){ - "rocksdb_opts": func(dataDir string) (corestore.KVStoreWithBatch, error) { - return dbm.NewRocksDB("test", dataDir) - }, "pebbledb_opts": func(dataDir string) (corestore.KVStoreWithBatch, error) { return dbm.NewPebbleDB("test", dataDir) }, diff --git a/store/v2/db/db.go b/store/v2/db/db.go index 3be46d750f39..dbe67e59792c 100644 --- a/store/v2/db/db.go +++ b/store/v2/db/db.go @@ -11,7 +11,6 @@ type DBType string const ( DBTypeGoLevelDB DBType = "goleveldb" - DBTypeRocksDB DBType = "rocksdb" DBTypePebbleDB DBType = "pebbledb" DBTypePrefixDB DBType = "prefixdb" diff --git a/store/v2/db/pebbledb.go b/store/v2/db/pebbledb.go index c4eb2c0122bf..e6f4d67a97bd 100644 --- a/store/v2/db/pebbledb.go +++ b/store/v2/db/pebbledb.go @@ -72,10 +72,6 @@ func (db *PebbleDB) Get(key []byte) ([]byte, error) { return nil, fmt.Errorf("failed to perform PebbleDB read: %w", err) } - if len(bz) == 0 { - return nil, closer.Close() - } - return bz, closer.Close() } diff --git a/store/v2/db/rocksdb.go b/store/v2/db/rocksdb.go deleted file mode 100644 index 5ef7e64da4e7..000000000000 --- a/store/v2/db/rocksdb.go +++ /dev/null @@ -1,348 +0,0 @@ -//go:build rocksdb -// +build rocksdb - -package db - -import ( - "bytes" - "fmt" - "path/filepath" - "runtime" - "slices" - - "github.com/linxGnu/grocksdb" - - corestore "cosmossdk.io/core/store" - storeerrors "cosmossdk.io/store/v2/errors" -) - -var ( - _ corestore.KVStoreWithBatch = (*RocksDB)(nil) - - defaultReadOpts = grocksdb.NewDefaultReadOptions() -) - -// RocksDB implements `corestore.KVStoreWithBatch`, using RocksDB as the underlying storage engine. -// It is only used for store v2 migration, since some clients use RocksDB as -// the IAVL v0/v1 backend. -type RocksDB struct { - storage *grocksdb.DB -} - -// defaultRocksdbOptions is good enough for most cases, including heavy workloads. -// 1GB table cache, 512MB write buffer(may use 50% more on heavy workloads). -// compression: snappy as default, use `-lsnappy` flag to enable. -func defaultRocksdbOptions() *grocksdb.Options { - bbto := grocksdb.NewDefaultBlockBasedTableOptions() - bbto.SetBlockCache(grocksdb.NewLRUCache(1 << 30)) - bbto.SetFilterPolicy(grocksdb.NewBloomFilter(10)) - - rocksdbOpts := grocksdb.NewDefaultOptions() - rocksdbOpts.SetBlockBasedTableFactory(bbto) - // SetMaxOpenFiles to 4096 seems to provide a reliable performance boost - rocksdbOpts.SetMaxOpenFiles(4096) - rocksdbOpts.SetCreateIfMissing(true) - rocksdbOpts.IncreaseParallelism(runtime.NumCPU()) - // 1.5GB maximum memory use for writebuffer. - rocksdbOpts.OptimizeLevelStyleCompaction(512 * 1024 * 1024) - return rocksdbOpts -} - -func NewRocksDB(name, dataDir string) (*RocksDB, error) { - opts := defaultRocksdbOptions() - opts.SetCreateIfMissing(true) - - return NewRocksDBWithOpts(name, dataDir, opts) -} - -func NewRocksDBWithOpts(name, dataDir string, opts *grocksdb.Options) (*RocksDB, error) { - dbPath := filepath.Join(dataDir, name+DBFileSuffix) - storage, err := grocksdb.OpenDb(opts, dbPath) - if err != nil { - return nil, fmt.Errorf("failed to open RocksDB: %w", err) - } - - return &RocksDB{ - storage: storage, - }, nil -} - -func (db *RocksDB) Close() error { - db.storage.Close() - db.storage = nil - return nil -} - -func (db *RocksDB) Get(key []byte) ([]byte, error) { - bz, err := db.storage.GetBytes(defaultReadOpts, key) - if err != nil { - return nil, err - } - - return bz, nil -} - -func (db *RocksDB) Has(key []byte) (bool, error) { - bz, err := db.Get(key) - if err != nil { - return false, err - } - - return bz != nil, nil -} - -func (db *RocksDB) Set(key, value []byte) error { - if len(key) == 0 { - return storeerrors.ErrKeyEmpty - } - if value == nil { - return storeerrors.ErrValueNil - } - - return db.storage.Put(grocksdb.NewDefaultWriteOptions(), key, value) -} - -func (db *RocksDB) Delete(key []byte) error { - if len(key) == 0 { - return storeerrors.ErrKeyEmpty - } - - return db.storage.Delete(grocksdb.NewDefaultWriteOptions(), key) -} - -func (db *RocksDB) Iterator(start, end []byte) (corestore.Iterator, error) { - if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) { - return nil, storeerrors.ErrKeyEmpty - } - - itr := db.storage.NewIterator(defaultReadOpts) - return newRocksDBIterator(itr, start, end, false), nil -} - -func (db *RocksDB) ReverseIterator(start, end []byte) (corestore.Iterator, error) { - if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) { - return nil, storeerrors.ErrKeyEmpty - } - - itr := db.storage.NewIterator(defaultReadOpts) - return newRocksDBIterator(itr, start, end, true), nil -} - -func (db *RocksDB) NewBatch() corestore.Batch { - return &rocksDBBatch{ - db: db, - batch: grocksdb.NewWriteBatch(), - } -} - -func (db *RocksDB) NewBatchWithSize(_ int) corestore.Batch { - return db.NewBatch() -} - -var _ corestore.Iterator = (*rocksDBIterator)(nil) - -type rocksDBIterator struct { - source *grocksdb.Iterator - start []byte - end []byte - valid bool - reverse bool -} - -func newRocksDBIterator(src *grocksdb.Iterator, start, end []byte, reverse bool) *rocksDBIterator { - if reverse { - if end == nil { - src.SeekToLast() - } else { - src.Seek(end) - - if src.Valid() { - eoaKey := readOnlySlice(src.Key()) // end or after key - if bytes.Compare(end, eoaKey) <= 0 { - src.Prev() - } - } else { - src.SeekToLast() - } - } - } else { - if start == nil { - src.SeekToFirst() - } else { - src.Seek(start) - } - } - - return &rocksDBIterator{ - source: src, - start: start, - end: end, - reverse: reverse, - valid: src.Valid(), - } -} - -func (itr *rocksDBIterator) Domain() (start, end []byte) { - return itr.start, itr.end -} - -func (itr *rocksDBIterator) Valid() bool { - // once invalid, forever invalid - if !itr.valid { - return false - } - - // if source has error, consider it invalid - if err := itr.source.Err(); err != nil { - itr.valid = false - return false - } - - // if source is invalid, consider it invalid - if !itr.source.Valid() { - itr.valid = false - return false - } - - // if key is at the end or past it, consider it invalid - start := itr.start - end := itr.end - key := readOnlySlice(itr.source.Key()) - - if itr.reverse { - if start != nil && bytes.Compare(key, start) < 0 { - itr.valid = false - return false - } - } else { - if end != nil && bytes.Compare(end, key) <= 0 { - itr.valid = false - return false - } - } - - return true -} - -func (itr *rocksDBIterator) Key() []byte { - itr.assertIsValid() - return copyAndFreeSlice(itr.source.Key()) -} - -func (itr *rocksDBIterator) Value() []byte { - itr.assertIsValid() - return copyAndFreeSlice(itr.source.Value()) -} - -func (itr *rocksDBIterator) Next() { - if !itr.valid { - return - } - - if itr.reverse { - itr.source.Prev() - } else { - itr.source.Next() - } -} - -func (itr *rocksDBIterator) Error() error { - return itr.source.Err() -} - -func (itr *rocksDBIterator) Close() error { - itr.source.Close() - itr.source = nil - itr.valid = false - - return nil -} - -func (itr *rocksDBIterator) assertIsValid() { - if !itr.valid { - panic("rocksDB iterator is invalid") - } -} - -type rocksDBBatch struct { - db *RocksDB - batch *grocksdb.WriteBatch -} - -func (b *rocksDBBatch) Set(key, value []byte) error { - if len(key) == 0 { - return storeerrors.ErrKeyEmpty - } - if value == nil { - return storeerrors.ErrValueNil - } - if b.batch == nil { - return storeerrors.ErrBatchClosed - } - - b.batch.Put(key, value) - return nil -} - -func (b *rocksDBBatch) Delete(key []byte) error { - if len(key) == 0 { - return storeerrors.ErrKeyEmpty - } - if b.batch == nil { - return storeerrors.ErrBatchClosed - } - - b.batch.Delete(key) - return nil -} - -func (b *rocksDBBatch) Write() error { - writeOpts := grocksdb.NewDefaultWriteOptions() - writeOpts.SetSync(false) - - if err := b.db.storage.Write(writeOpts, b.batch); err != nil { - return fmt.Errorf("failed to write RocksDB batch: %w", err) - } - - return nil -} - -func (b *rocksDBBatch) WriteSync() error { - writeOpts := grocksdb.NewDefaultWriteOptions() - writeOpts.SetSync(true) - - if err := b.db.storage.Write(writeOpts, b.batch); err != nil { - return fmt.Errorf("failed to write RocksDB batch: %w", err) - } - - return nil -} - -func (b *rocksDBBatch) Close() error { - b.batch.Destroy() - return nil -} - -func (b *rocksDBBatch) GetByteSize() (int, error) { - return len(b.batch.Data()), nil -} - -func readOnlySlice(s *grocksdb.Slice) []byte { - if !s.Exists() { - return nil - } - - return s.Data() -} - -// copyAndFreeSlice will copy a given RocksDB slice and free it. If the slice -// does not exist, will be returned. -func copyAndFreeSlice(s *grocksdb.Slice) []byte { - defer s.Free() - - if !s.Exists() { - return nil - } - - return slices.Clone(s.Data()) -} diff --git a/store/v2/db/rocksdb_noflag.go b/store/v2/db/rocksdb_noflag.go deleted file mode 100644 index ab6ecba70753..000000000000 --- a/store/v2/db/rocksdb_noflag.go +++ /dev/null @@ -1,60 +0,0 @@ -//go:build !rocksdb -// +build !rocksdb - -package db - -import ( - coreserver "cosmossdk.io/core/server" - corestore "cosmossdk.io/core/store" -) - -var _ corestore.KVStoreWithBatch = (*RocksDB)(nil) - -// RocksDB implements `corestore.KVStoreWithBatch` using RocksDB as the underlying storage engine. -// It is used for only store v2 migration, since some clients use RocksDB as -// the IAVL v0/v1 backend. -type RocksDB struct{} - -func NewRocksDB(name, dataDir string) (*RocksDB, error) { - panic("rocksdb must be built with -tags rocksdb") -} - -func NewRocksDBWithOpts(dataDir string, opts coreserver.DynamicConfig) (*RocksDB, error) { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) Close() error { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) Get(key []byte) ([]byte, error) { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) Has(key []byte) (bool, error) { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) Set(key, value []byte) error { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) Delete(key []byte) error { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) Iterator(start, end []byte) (corestore.Iterator, error) { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) ReverseIterator(start, end []byte) (corestore.Iterator, error) { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) NewBatch() corestore.Batch { - panic("rocksdb must be built with -tags rocksdb") -} - -func (db *RocksDB) NewBatchWithSize(_ int) corestore.Batch { - panic("rocksdb must be built with -tags rocksdb") -} diff --git a/store/v2/db/rocksdb_test.go b/store/v2/db/rocksdb_test.go deleted file mode 100644 index 75147a56855d..000000000000 --- a/store/v2/db/rocksdb_test.go +++ /dev/null @@ -1,20 +0,0 @@ -//go:build rocksdb -// +build rocksdb - -package db - -import ( - "testing" - - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" -) - -func TestRocksDBSuite(t *testing.T) { - db, err := NewRocksDB("test", t.TempDir()) - require.NoError(t, err) - - suite.Run(t, &DBTestSuite{ - db: db, - }) -} diff --git a/store/v2/go.mod b/store/v2/go.mod index b2fe4ca936c2..6223bfc08e4e 100644 --- a/store/v2/go.mod +++ b/store/v2/go.mod @@ -15,7 +15,6 @@ require ( github.com/cosmos/ics23/go v0.11.0 github.com/google/btree v1.1.3 github.com/hashicorp/go-metrics v0.5.3 - github.com/linxGnu/grocksdb v1.9.3 github.com/spf13/cast v1.7.1 github.com/stretchr/testify v1.10.0 github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d diff --git a/store/v2/go.sum b/store/v2/go.sum index 3cebafb2b28f..45aa36096be2 100644 --- a/store/v2/go.sum +++ b/store/v2/go.sum @@ -161,8 +161,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/linxGnu/grocksdb v1.9.3 h1:s1cbPcOd0cU2SKXRG1nEqCOWYAELQjdqg3RVI2MH9ik= -github.com/linxGnu/grocksdb v1.9.3/go.mod h1:QYiYypR2d4v63Wj1adOOfzglnoII0gLj3PNh4fZkcFA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=