Skip to content

Commit

Permalink
Avoid repeated called of NewIterator which might be slow (#718)
Browse files Browse the repository at this point in the history
# Conflicts:
#	pump/storage/storage.go
  • Loading branch information
leoppro committed Aug 22, 2019
1 parent d736bce commit 22c2939
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pump/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pingcap/tidb/store/tikv/oracle"
pb "github.com/pingcap/tipb/go-binlog"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -594,7 +595,13 @@ func (a *Append) doGCTS(ts int64) {
Start: encodeTSKey(0),
Limit: encodeTSKey(ts + 1),
}
iter := a.metadata.NewIterator(irange, nil)
var iter iterator.Iterator
defer func() {
if iter != nil {
iter.Release()
iter = nil
}
}()

for {
nStr, err := a.metadata.GetProperty("leveldb.num-files-at-level0")
Expand All @@ -611,13 +618,24 @@ func (a *Append) doGCTS(ts int64) {

if l0Num >= l0Trigger {
log.Infof("wait some time to gc cause too many L0 file, files: %d", l0Num)
if iter != nil {
iter.Release()
iter = nil
}
time.Sleep(5 * time.Second)
continue
}

deleteBatch := 0
var lastKey []byte

if iter == nil {
log.Infof("New LevelDB iterator created for GC, start: %d, limit: %d",
decodeTSKey(irange.Start),
decodeTSKey(irange.Limit))
iter = a.metadata.NewIterator(irange, nil)
}

for iter.Next() && deleteBatch < 100 {
batch.Delete(iter.Key())
deleteNum++
Expand Down Expand Up @@ -647,15 +665,14 @@ func (a *Append) doGCTS(ts int64) {
}

if len(lastKey) > 0 {
irange.Start = lastKey
a.vlog.gcTS(decodeTSKey(lastKey))
doneGcTSGauge.Set(float64(oracle.ExtractPhysical(uint64(decodeTSKey(lastKey)))))
}

log.Infof("has delete %d number", deleteNum)
}

iter.Release()

a.vlog.gcTS(ts)
doneGcTSGauge.Set(float64(oracle.ExtractPhysical(uint64(ts))))
log.Infof("finish gc, ts: %d delete num: %d", ts, deleteNum)
Expand Down

0 comments on commit 22c2939

Please sign in to comment.