Skip to content

Commit 27088ac

Browse files
committed
Replace RocksDB by goleveldb
https://jira.hyperledger.org/browse/FAB-788 RocksDB has a patent infringement license in it from Facebook (https://github.com/facebook/rocksdb/blob/master/PATENTS) Many users may not be comfortable with the term of the license. The alternatives include 1. LevelDB (https://github.com/google/leveldb) with a go wrapper (https://github.com/jmhodges/levigo), 2. goleveldb (https://github.com/syndtr/goleveldb) - a porting of leveldb in golang 3. BoltDB (https://github.com/boltdb/bolt) BoltDB is suitable for read heavy workloads (e.g., LDAP) but has a relatively poor performance for read-write workloads. Of the other two options, goleveldb is chosen because it is implemented in golang and hence easy to intergate and maintain. In addition, as a precedent, ethereum go implementation also uses this package https://github.com/ethereum/go-ethereum/blob/master/ethdb/database.go Change-Id: Ia4fb5a6f9299e613d03d8b414a51bf479bfafd59 Signed-off-by: manish <[email protected]>
1 parent 8c9dcc9 commit 27088ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+14347
-157
lines changed

core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/hyperledger/fabric/core/ledger/util/db"
2828
"github.com/hyperledger/fabric/protos"
2929
"github.com/op/go-logging"
30-
"github.com/tecbot/gorocksdb"
3130
)
3231

3332
var logger = logging.MustGetLogger("kvledger")
@@ -45,7 +44,6 @@ type blockfileMgr struct {
4544
rootDir string
4645
conf *Conf
4746
db *db.DB
48-
defaultCF *gorocksdb.ColumnFamilyHandle
4947
index index
5048
cpInfo *checkpointInfo
5149
cpInfoCond *sync.Cond
@@ -60,7 +58,7 @@ func newBlockfileMgr(conf *Conf, indexConfig *blkstorage.IndexConfig) *blockfile
6058
panic(fmt.Sprintf("Error: %s", err))
6159
}
6260
db := initDB(conf)
63-
mgr := &blockfileMgr{rootDir: rootDir, conf: conf, db: db, defaultCF: db.GetDefaultCFHandle()}
61+
mgr := &blockfileMgr{rootDir: rootDir, conf: conf, db: db}
6462
cpInfo, err := mgr.loadCurrentInfo()
6563
if err != nil {
6664
panic(fmt.Sprintf("Could not get block file info for current block file from db: %s", err))
@@ -82,7 +80,7 @@ func newBlockfileMgr(conf *Conf, indexConfig *blkstorage.IndexConfig) *blockfile
8280
panic(fmt.Sprintf("Could not truncate current file to known size in db: %s", err))
8381
}
8482

85-
mgr.index = newBlockIndex(indexConfig, db, db.GetCFHandle(blockIndexCF))
83+
mgr.index = newBlockIndex(indexConfig, db)
8684
mgr.cpInfo = cpInfo
8785
mgr.currentFileWriter = currentFileWriter
8886
mgr.cpInfoCond = sync.NewCond(&sync.Mutex{})
@@ -115,10 +113,7 @@ func newBlockfileMgr(conf *Conf, indexConfig *blkstorage.IndexConfig) *blockfile
115113

116114
func initDB(conf *Conf) *db.DB {
117115
dbInst := db.CreateDB(&db.Conf{
118-
DBPath: conf.dbPath,
119-
CFNames: []string{blockIndexCF},
120-
DisableWAL: true})
121-
116+
DBPath: conf.dbPath})
122117
dbInst.Open()
123118
return dbInst
124119
}
@@ -421,7 +416,7 @@ func (mgr *blockfileMgr) fetchRawBytes(lp *fileLocPointer) ([]byte, error) {
421416
func (mgr *blockfileMgr) loadCurrentInfo() (*checkpointInfo, error) {
422417
var b []byte
423418
var err error
424-
if b, err = mgr.db.Get(mgr.defaultCF, blkMgrInfoKey); b == nil || err != nil {
419+
if b, err = mgr.db.Get(blkMgrInfoKey); b == nil || err != nil {
425420
return nil, err
426421
}
427422
i := &checkpointInfo{}
@@ -432,20 +427,14 @@ func (mgr *blockfileMgr) loadCurrentInfo() (*checkpointInfo, error) {
432427
return i, nil
433428
}
434429

435-
func (mgr *blockfileMgr) saveCurrentInfo(i *checkpointInfo, flush bool) error {
430+
func (mgr *blockfileMgr) saveCurrentInfo(i *checkpointInfo, sync bool) error {
436431
b, err := i.marshal()
437432
if err != nil {
438433
return err
439434
}
440-
if err = mgr.db.Put(mgr.defaultCF, blkMgrInfoKey, b); err != nil {
435+
if err = mgr.db.Put(blkMgrInfoKey, b, sync); err != nil {
441436
return err
442437
}
443-
if flush {
444-
if err = mgr.db.Flush(true); err != nil {
445-
return err
446-
}
447-
logger.Debugf("saved checkpointInfo:%s", i)
448-
}
449438
return nil
450439
}
451440

core/ledger/blkstorage/fsblkstorage/blockindex.go

+13-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/hyperledger/fabric/core/ledger/blkstorage"
2424
"github.com/hyperledger/fabric/core/ledger/util"
2525
"github.com/hyperledger/fabric/core/ledger/util/db"
26-
"github.com/tecbot/gorocksdb"
26+
"github.com/syndtr/goleveldb/leveldb"
2727
)
2828

2929
const (
@@ -53,24 +53,22 @@ type blockIdxInfo struct {
5353
type blockIndex struct {
5454
indexItemsMap map[blkstorage.IndexableAttr]bool
5555
db *db.DB
56-
blockIndexCF *gorocksdb.ColumnFamilyHandle
5756
}
5857

59-
func newBlockIndex(indexConfig *blkstorage.IndexConfig, db *db.DB,
60-
indexCFHandle *gorocksdb.ColumnFamilyHandle) *blockIndex {
58+
func newBlockIndex(indexConfig *blkstorage.IndexConfig, db *db.DB) *blockIndex {
6159
indexItems := indexConfig.AttrsToIndex
6260
logger.Debugf("newBlockIndex() - indexItems:[%s]", indexItems)
6361
indexItemsMap := make(map[blkstorage.IndexableAttr]bool)
6462
for _, indexItem := range indexItems {
6563
indexItemsMap[indexItem] = true
6664
}
67-
return &blockIndex{indexItemsMap, db, indexCFHandle}
65+
return &blockIndex{indexItemsMap, db}
6866
}
6967

7068
func (index *blockIndex) getLastBlockIndexed() (uint64, error) {
7169
var blockNumBytes []byte
7270
var err error
73-
if blockNumBytes, err = index.db.Get(index.blockIndexCF, indexCheckpointKey); err != nil {
71+
if blockNumBytes, err = index.db.Get(indexCheckpointKey); err != nil {
7472
return 0, nil
7573
}
7674
return decodeBlockNum(blockNumBytes), nil
@@ -85,19 +83,18 @@ func (index *blockIndex) indexBlock(blockIdxInfo *blockIdxInfo) error {
8583
logger.Debugf("Indexing block [%s]", blockIdxInfo)
8684
flp := blockIdxInfo.flp
8785
txOffsets := blockIdxInfo.txOffsets
88-
batch := gorocksdb.NewWriteBatch()
89-
defer batch.Destroy()
86+
batch := &leveldb.Batch{}
9087
flpBytes, err := flp.marshal()
9188
if err != nil {
9289
return err
9390
}
9491

9592
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrBlockHash]; ok {
96-
batch.PutCF(index.blockIndexCF, constructBlockHashKey(blockIdxInfo.blockHash), flpBytes)
93+
batch.Put(constructBlockHashKey(blockIdxInfo.blockHash), flpBytes)
9794
}
9895

9996
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrBlockNum]; ok {
100-
batch.PutCF(index.blockIndexCF, constructBlockNumKey(blockIdxInfo.blockNum), flpBytes)
97+
batch.Put(constructBlockNumKey(blockIdxInfo.blockNum), flpBytes)
10198
}
10299

103100
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrTxID]; ok {
@@ -110,12 +107,12 @@ func (index *blockIndex) indexBlock(blockIdxInfo *blockIdxInfo) error {
110107
if marshalErr != nil {
111108
return marshalErr
112109
}
113-
batch.PutCF(index.blockIndexCF, constructTxIDKey(txID), txFlpBytes)
110+
batch.Put(constructTxIDKey(txID), txFlpBytes)
114111
}
115112
}
116113

117-
batch.PutCF(index.blockIndexCF, indexCheckpointKey, encodeBlockNum(blockIdxInfo.blockNum))
118-
if err := index.db.WriteBatch(batch); err != nil {
114+
batch.Put(indexCheckpointKey, encodeBlockNum(blockIdxInfo.blockNum))
115+
if err := index.db.WriteBatch(batch, false); err != nil {
119116
return err
120117
}
121118
return nil
@@ -125,7 +122,7 @@ func (index *blockIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, e
125122
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrBlockHash]; !ok {
126123
return nil, blkstorage.ErrAttrNotIndexed
127124
}
128-
b, err := index.db.Get(index.blockIndexCF, constructBlockHashKey(blockHash))
125+
b, err := index.db.Get(constructBlockHashKey(blockHash))
129126
if err != nil {
130127
return nil, err
131128
}
@@ -141,7 +138,7 @@ func (index *blockIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer
141138
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrBlockNum]; !ok {
142139
return nil, blkstorage.ErrAttrNotIndexed
143140
}
144-
b, err := index.db.Get(index.blockIndexCF, constructBlockNumKey(blockNum))
141+
b, err := index.db.Get(constructBlockNumKey(blockNum))
145142
if err != nil {
146143
return nil, err
147144
}
@@ -157,7 +154,7 @@ func (index *blockIndex) getTxLoc(txID string) (*fileLocPointer, error) {
157154
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrTxID]; !ok {
158155
return nil, blkstorage.ErrAttrNotIndexed
159156
}
160-
b, err := index.db.Get(index.blockIndexCF, constructTxIDKey(txID))
157+
b, err := index.db.Get(constructTxIDKey(txID))
161158
if err != nil {
162159
return nil, err
163160
}

core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb_txmgr.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/hyperledger/fabric/protos"
2929
putils "github.com/hyperledger/fabric/protos/utils"
3030
"github.com/op/go-logging"
31-
"github.com/tecbot/gorocksdb"
3231
)
3332

3433
var logger = logging.MustGetLogger("couchdbtxmgmt")
@@ -68,7 +67,6 @@ func (u *updateSet) get(compositeKey []byte) *versionedValue {
6867
// This implementation uses a read-write lock to prevent conflicts between transaction simulation and committing
6968
type CouchDBTxMgr struct {
7069
db *db.DB
71-
stateIndexCF *gorocksdb.ColumnFamilyHandle
7270
updateSet *updateSet
7371
commitRWLock sync.RWMutex
7472
couchDB *couchdb.CouchDBConnectionDef // COUCHDB new properties for CouchDB
@@ -87,7 +85,7 @@ type CouchConnection struct {
8785
func NewCouchDBTxMgr(conf *Conf, host string, port int, dbName string, id string, pw string) *CouchDBTxMgr {
8886

8987
// TODO cleanup this RocksDB handle
90-
db := db.CreateDB(&db.Conf{DBPath: conf.DBPath, CFNames: []string{}})
88+
db := db.CreateDB(&db.Conf{DBPath: conf.DBPath})
9189
db.Open()
9290

9391
couchDB, err := couchdb.CreateConnectionDefinition(host,
@@ -106,7 +104,7 @@ func NewCouchDBTxMgr(conf *Conf, host string, port int, dbName string, id string
106104
}
107105

108106
// db and stateIndexCF will not be used for CouchDB. TODO to cleanup
109-
return &CouchDBTxMgr{db: db, stateIndexCF: db.GetDefaultCFHandle(), couchDB: couchDB}
107+
return &CouchDBTxMgr{db: db, couchDB: couchDB}
110108
}
111109

112110
// NewQueryExecutor implements method in interface `txmgmt.TxMgr`

core/ledger/kvledger/txmgmt/lockbasedtxmgmt/lockbased_txmgr.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"github.com/hyperledger/fabric/protos"
2828
putils "github.com/hyperledger/fabric/protos/utils"
2929
"github.com/op/go-logging"
30-
"github.com/tecbot/gorocksdb"
30+
"github.com/syndtr/goleveldb/leveldb"
3131
)
3232

3333
var logger = logging.MustGetLogger("lockbasedtxmgmt")
@@ -67,16 +67,15 @@ func (u *updateSet) get(compositeKey []byte) *versionedValue {
6767
// This implementation uses a read-write lock to prevent conflicts between transaction simulation and committing
6868
type LockBasedTxMgr struct {
6969
db *db.DB
70-
stateIndexCF *gorocksdb.ColumnFamilyHandle
7170
updateSet *updateSet
7271
commitRWLock sync.RWMutex
7372
}
7473

7574
// NewLockBasedTxMgr constructs a `LockBasedTxMgr`
7675
func NewLockBasedTxMgr(conf *Conf) *LockBasedTxMgr {
77-
db := db.CreateDB(&db.Conf{DBPath: conf.DBPath, CFNames: []string{}})
76+
db := db.CreateDB(&db.Conf{DBPath: conf.DBPath})
7877
db.Open()
79-
return &LockBasedTxMgr{db: db, stateIndexCF: db.GetDefaultCFHandle()}
78+
return &LockBasedTxMgr{db: db}
8079
}
8180

8281
// NewQueryExecutor implements method in interface `txmgmt.TxMgr`
@@ -226,18 +225,17 @@ func (txmgr *LockBasedTxMgr) addWriteSetToBatch(txRWSet *txmgmt.TxReadWriteSet)
226225

227226
// Commit implements method in interface `txmgmt.TxMgr`
228227
func (txmgr *LockBasedTxMgr) Commit() error {
229-
batch := gorocksdb.NewWriteBatch()
228+
batch := &leveldb.Batch{}
230229
if txmgr.updateSet == nil {
231230
panic("validateAndPrepare() method should have been called before calling commit()")
232231
}
233232
for k, v := range txmgr.updateSet.m {
234-
batch.PutCF(txmgr.stateIndexCF, []byte(k), encodeValue(v.value, v.version))
233+
batch.Put([]byte(k), encodeValue(v.value, v.version))
235234
}
236235
txmgr.commitRWLock.Lock()
237236
defer txmgr.commitRWLock.Unlock()
238237
defer func() { txmgr.updateSet = nil }()
239-
defer batch.Destroy()
240-
if err := txmgr.db.WriteBatch(batch); err != nil {
238+
if err := txmgr.db.WriteBatch(batch, false); err != nil {
241239
return err
242240
}
243241
return nil
@@ -261,7 +259,7 @@ func (txmgr *LockBasedTxMgr) getCommittedValueAndVersion(ns string, key string)
261259
compositeKey := constructCompositeKey(ns, key)
262260
var encodedValue []byte
263261
var err error
264-
if encodedValue, err = txmgr.db.Get(txmgr.stateIndexCF, compositeKey); err != nil {
262+
if encodedValue, err = txmgr.db.Get(compositeKey); err != nil {
265263
return nil, 0, err
266264
}
267265
if encodedValue == nil {

0 commit comments

Comments
 (0)