Write data to DB synchronously in bulk connect

RocksDB has some memory leak/fragmentation problem
when inserting data in parallel
This commit is contained in:
Martin Boehm 2018-08-21 18:56:30 +02:00
parent c9471bf867
commit 188eed8881

View File

@ -285,12 +285,18 @@ func (d *RocksDB) writeBlock(block *bchain.Block, op int) error {
return d.db.Write(d.wo, wb) return d.db.Write(d.wo, wb)
} }
// BulkConnect is used to connect blocks in bulk, faster but if interrupted inconsistent way // bulk connect
// in bulk mode the data are cached and stored to db in batches
// it speeds up the import in two ways:
// 1) balances and txAddresses are modified several times during the import, there is a chance that the modifications are done before write to DB
// 2) rocksdb seems to handle better fewer larger batches than continuous stream of smaller batches
type bulkAddresses struct { type bulkAddresses struct {
height uint32 height uint32
addresses map[string][]outpoint addresses map[string][]outpoint
} }
// BulkConnect is used to connect blocks in bulk, faster but if interrupted inconsistent way
type BulkConnect struct { type BulkConnect struct {
d *RocksDB d *RocksDB
isUTXO bool isUTXO bool
@ -302,13 +308,14 @@ type BulkConnect struct {
} }
const ( const (
maxBulkAddresses = 400000 maxBulkAddresses = 300000
maxBulkTxAddresses = 2000000 maxBulkTxAddresses = 2000000
partialStoreAddresses = maxBulkTxAddresses / 10 partialStoreAddresses = maxBulkTxAddresses / 10
maxBulkBalances = 2500000 maxBulkBalances = 2500000
partialStoreBalances = maxBulkBalances / 10 partialStoreBalances = maxBulkBalances / 10
) )
// InitBulkConnect initializes bulk connect and switches DB to inconsistent state
func (d *RocksDB) InitBulkConnect() (*BulkConnect, error) { func (d *RocksDB) InitBulkConnect() (*BulkConnect, error) {
bc := &BulkConnect{ bc := &BulkConnect{
d: d, d: d,
@ -323,9 +330,7 @@ func (d *RocksDB) InitBulkConnect() (*BulkConnect, error) {
return bc, nil return bc, nil
} }
func (b *BulkConnect) storeTxAddresses(c chan error, all bool) { func (b *BulkConnect) storeTxAddresses(wb *gorocksdb.WriteBatch, all bool) (int, int, error) {
defer close(c)
start := time.Now()
var txm map[string]*TxAddresses var txm map[string]*TxAddresses
var sp int var sp int
if all { if all {
@ -359,21 +364,31 @@ func (b *BulkConnect) storeTxAddresses(c chan error, all bool) {
} }
} }
} }
wb := gorocksdb.NewWriteBatch()
defer wb.Destroy()
if err := b.d.storeTxAddresses(wb, txm); err != nil { if err := b.d.storeTxAddresses(wb, txm); err != nil {
c <- err return 0, 0, err
} else {
if err := b.d.db.Write(b.d.wo, wb); err != nil {
c <- err
}
} }
glog.Info("rocksdb: height ", b.height, ", stored ", len(txm), " (", sp, " spent) txAddresses, ", len(b.txAddressesMap), " remaining, done in ", time.Since(start)) return len(txm), sp, nil
} }
func (b *BulkConnect) storeBalances(c chan error, all bool) { func (b *BulkConnect) parallelStoreTxAddresses(c chan error, all bool) {
defer close(c) defer close(c)
start := time.Now() start := time.Now()
wb := gorocksdb.NewWriteBatch()
defer wb.Destroy()
count, sp, err := b.storeTxAddresses(wb, all)
if err != nil {
c <- err
return
}
if err := b.d.db.Write(b.d.wo, wb); err != nil {
c <- err
return
}
glog.Info("rocksdb: height ", b.height, ", stored ", count, " (", sp, " spent) txAddresses, ", len(b.txAddressesMap), " remaining, done in ", time.Since(start))
c <- nil
}
func (b *BulkConnect) storeBalances(wb *gorocksdb.WriteBatch, all bool) (int, error) {
var bal map[string]*AddrBalance var bal map[string]*AddrBalance
if all { if all {
bal = b.balances bal = b.balances
@ -389,16 +404,28 @@ func (b *BulkConnect) storeBalances(c chan error, all bool) {
} }
} }
} }
if err := b.d.storeBalances(wb, bal); err != nil {
return 0, err
}
return len(bal), nil
}
func (b *BulkConnect) parallelStoreBalances(c chan error, all bool) {
defer close(c)
start := time.Now()
wb := gorocksdb.NewWriteBatch() wb := gorocksdb.NewWriteBatch()
defer wb.Destroy() defer wb.Destroy()
if err := b.d.storeBalances(wb, bal); err != nil { count, err := b.storeBalances(wb, all)
if err != nil {
c <- err c <- err
} else { return
if err := b.d.db.Write(b.d.wo, wb); err != nil {
c <- err
}
} }
glog.Info("rocksdb: height ", b.height, ", stored ", len(bal), " balances, ", len(b.balances), " remaining, done in ", time.Since(start)) if err := b.d.db.Write(b.d.wo, wb); err != nil {
c <- err
return
}
glog.Info("rocksdb: height ", b.height, ", stored ", count, " balances, ", len(b.balances), " remaining, done in ", time.Since(start))
c <- nil
} }
func (b *BulkConnect) storeBulkAddresses(wb *gorocksdb.WriteBatch) error { func (b *BulkConnect) storeBulkAddresses(wb *gorocksdb.WriteBatch) error {
@ -412,41 +439,46 @@ func (b *BulkConnect) storeBulkAddresses(wb *gorocksdb.WriteBatch) error {
return nil return nil
} }
// ConnectBlock connects block in bulk mode
func (b *BulkConnect) ConnectBlock(block *bchain.Block, storeBlockTxs bool) error { func (b *BulkConnect) ConnectBlock(block *bchain.Block, storeBlockTxs bool) error {
b.height = block.Height b.height = block.Height
if !b.isUTXO { if !b.isUTXO {
return b.d.ConnectBlock(block) return b.d.ConnectBlock(block)
} }
wb := gorocksdb.NewWriteBatch()
defer wb.Destroy()
addresses := make(map[string][]outpoint) addresses := make(map[string][]outpoint)
if err := b.d.processAddressesUTXO(block, addresses, b.txAddressesMap, b.balances); err != nil { if err := b.d.processAddressesUTXO(block, addresses, b.txAddressesMap, b.balances); err != nil {
return err return err
} }
start := time.Now()
var sa bool
var storeAddressesChan, storeBalancesChan chan error
if len(b.txAddressesMap) > maxBulkTxAddresses || len(b.balances) > maxBulkBalances {
sa = true
if len(b.txAddressesMap)+partialStoreAddresses > maxBulkTxAddresses {
storeAddressesChan = make(chan error)
go b.storeTxAddresses(storeAddressesChan, false)
}
if len(b.balances)+partialStoreBalances > maxBulkBalances {
storeBalancesChan = make(chan error)
go b.storeBalances(storeBalancesChan, false)
}
}
b.bulkAddresses = append(b.bulkAddresses, bulkAddresses{ b.bulkAddresses = append(b.bulkAddresses, bulkAddresses{
height: block.Height, height: block.Height,
addresses: addresses, addresses: addresses,
}) })
b.bulkAddressesCount += len(addresses) b.bulkAddressesCount += len(addresses)
bac := b.bulkAddressesCount wb := gorocksdb.NewWriteBatch()
if sa || b.bulkAddressesCount > maxBulkAddresses { defer wb.Destroy()
if err := b.storeBulkAddresses(wb); err != nil { start := time.Now()
return err var count, count1, storeType int
} var err error
const (
storeNone = iota
storeTxAddresses
storeBalances
storeBulkAddresses
)
// in one block store maximally one type of data
if len(b.txAddressesMap) > maxBulkTxAddresses {
storeType = storeTxAddresses
count, count1, err = b.storeTxAddresses(wb, false)
} else if len(b.balances) > maxBulkBalances {
storeType = storeBalances
count, err = b.storeBalances(wb, false)
} else if b.bulkAddressesCount > maxBulkAddresses {
storeType = storeBulkAddresses
count = b.bulkAddressesCount
err = b.storeBulkAddresses(wb)
}
if err != nil {
return err
} }
if storeBlockTxs { if storeBlockTxs {
if err := b.d.storeAndCleanupBlockTxs(wb, block); err != nil { if err := b.d.storeAndCleanupBlockTxs(wb, block); err != nil {
@ -459,29 +491,26 @@ func (b *BulkConnect) ConnectBlock(block *bchain.Block, storeBlockTxs bool) erro
if err := b.d.db.Write(b.d.wo, wb); err != nil { if err := b.d.db.Write(b.d.wo, wb); err != nil {
return err return err
} }
if bac > b.bulkAddressesCount { switch storeType {
glog.Info("rocksdb: height ", b.height, ", stored ", bac, " addresses, done in ", time.Since(start)) case storeTxAddresses:
} glog.Info("rocksdb: height ", b.height, ", stored ", count, " (", count1, " spent) txAddresses, ", len(b.txAddressesMap), " remaining, done in ", time.Since(start))
if storeAddressesChan != nil { case storeBalances:
if err := <-storeAddressesChan; err != nil { glog.Info("rocksdb: height ", b.height, ", stored ", count, " balances, ", len(b.balances), " remaining, done in ", time.Since(start))
return err case storeBulkAddresses:
} glog.Info("rocksdb: height ", b.height, ", stored ", count, " addresses, done in ", time.Since(start))
}
if storeBalancesChan != nil {
if err := <-storeBalancesChan; err != nil {
return err
}
} }
return nil return nil
} }
// Close flushes the cached data and switches DB from inconsistent state open
// after Close, the BulkConnect cannot be used
func (b *BulkConnect) Close() error { func (b *BulkConnect) Close() error {
glog.Info("rocksdb: bulk connect closing") glog.Info("rocksdb: bulk connect closing")
start := time.Now() start := time.Now()
storeAddressesChan := make(chan error) storeAddressesChan := make(chan error)
go b.storeTxAddresses(storeAddressesChan, true) go b.parallelStoreTxAddresses(storeAddressesChan, true)
storeBalancesChan := make(chan error) storeBalancesChan := make(chan error)
go b.storeBalances(storeBalancesChan, true) go b.parallelStoreBalances(storeBalancesChan, true)
wb := gorocksdb.NewWriteBatch() wb := gorocksdb.NewWriteBatch()
defer wb.Destroy() defer wb.Destroy()
bac := b.bulkAddressesCount bac := b.bulkAddressesCount
@ -634,6 +663,7 @@ func (d *RocksDB) processAddressesUTXO(block *bchain.Block, addresses map[string
spendingTxid := blockTxIDs[txi] spendingTxid := blockTxIDs[txi]
ta := blockTxAddresses[txi] ta := blockTxAddresses[txi]
ta.Inputs = make([]TxInput, len(tx.Vin)) ta.Inputs = make([]TxInput, len(tx.Vin))
logged := false
for i, input := range tx.Vin { for i, input := range tx.Vin {
tai := &ta.Inputs[i] tai := &ta.Inputs[i]
btxID, err := d.chainParser.PackTxid(input.Txid) btxID, err := d.chainParser.PackTxid(input.Txid)
@ -670,7 +700,10 @@ func (d *RocksDB) processAddressesUTXO(block *bchain.Block, addresses map[string
// mark the output as spent in tx // mark the output as spent in tx
ot.Spent = true ot.Spent = true
if len(ot.addrID) == 0 { if len(ot.addrID) == 0 {
glog.Warningf("rocksdb: height %d, tx %v, input tx %v vout %v skipping empty address", block.Height, tx.Txid, input.Txid, input.Vout) if !logged {
glog.Warningf("rocksdb: height %d, tx %v, input tx %v vout %v skipping empty address", block.Height, tx.Txid, input.Txid, input.Vout)
logged = true
}
continue continue
} }
strAddrID := string(ot.addrID) strAddrID := string(ot.addrID)