Refresh iterator in DisconnectBlocks to avoid huge snapshot

This commit is contained in:
Martin Boehm 2018-02-05 18:35:05 +01:00
parent fdd9c9b99f
commit ee663944e2

View File

@ -416,14 +416,23 @@ func (d *RocksDB) DisconnectBlocks(
higher uint32, higher uint32,
) error { ) error {
glog.Infof("rocksdb: disconnecting blocks %d-%d", lower, higher) glog.Infof("rocksdb: disconnecting blocks %d-%d", lower, higher)
it := d.db.NewIteratorCF(d.ro, d.cfh[cfOutputs])
defer it.Close()
outputKeys := [][]byte{} outputKeys := [][]byte{}
outputValues := [][]byte{} outputValues := [][]byte{}
var totalOutputs uint64 var totalOutputs, count uint64
for it.SeekToFirst(); it.Valid(); it.Next() { var seekKey []byte
for {
var key []byte
it := d.db.NewIteratorCF(d.ro, d.cfh[cfOutputs])
if totalOutputs == 0 {
it.SeekToFirst()
} else {
it.Seek(seekKey)
it.Next()
}
for count = 0; it.Valid() && count < 1000000; it.Next() {
totalOutputs++ totalOutputs++
key := it.Key().Data() count++
key = it.Key().Data()
l := len(key) l := len(key)
if l > 4 { if l > 4 {
height := unpackUint(key[l-4 : l]) height := unpackUint(key[l-4 : l])
@ -438,6 +447,14 @@ func (d *RocksDB) DisconnectBlocks(
} }
} }
} }
seekKey = make([]byte, len(key))
copy(seekKey, key)
valid := it.Valid()
it.Close()
if !valid {
break
}
}
glog.Infof("rocksdb: about to disconnect %d outputs from %d", len(outputKeys), totalOutputs) glog.Infof("rocksdb: about to disconnect %d outputs from %d", len(outputKeys), totalOutputs)
wb := gorocksdb.NewWriteBatch() wb := gorocksdb.NewWriteBatch()
defer wb.Destroy() defer wb.Destroy()