Skip to content

Commit

Permalink
Fix ever existing iterator and transaction in write benchmark (dgraph…
Browse files Browse the repository at this point in the history
…-io#1457)

91a80ef (Add support collecting stats (sst count, vlog count, file sizes, move key count, valid keys etc) introduced count of various types of keys in the report (log) of the benchmark write tool. For counting various keys, iterators were used in the function `reportStats()` and the keys were counted periodically. For closing the iterators `defer it.close()` was used, which never closed iterators until `reportStats()` returned. This caused the iterators to accumulate and hence resulting in excessive memory usage. 

Now, this key count is made optional by the introduction of a new option `--show-keys`, which is disabled by default. The statistics is collected in a separate function and the iterators are properly closed after each iteration cycle. Also the transaction is properly discarded after each run.
  • Loading branch information
ahsanbarkati authored Aug 14, 2020
1 parent 763e7d7 commit d31355d
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions badger/cmd/write_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,11 @@ var (
compression bool
showDir bool
ttlDuration string
showKeysCount bool

internalKeyCount uint32
moveKeyCount uint32
invalidKeyCount uint32
validKeyCount uint32
sstCount uint32
vlogCount uint32
files []string
sstCount uint32
vlogCount uint32
files []string

dropAllPeriod string
dropPrefixPeriod string
Expand Down Expand Up @@ -133,6 +130,8 @@ func init() {
"TTL duration in seconds for the entries, 0 means without TTL")
writeBenchCmd.Flags().StringVarP(&gcPeriod, "gc-every", "g", "5m", "GC Period.")
writeBenchCmd.Flags().Float64VarP(&gcDiscardRatio, "gc-ratio", "r", 0.5, "GC discard ratio.")
writeBenchCmd.Flags().BoolVar(&showKeysCount, "show-keys", false,
"If true, the report will include the keys statistics")
}

func writeRandom(db *badger.DB, num uint64) error {
Expand Down Expand Up @@ -298,6 +297,42 @@ func writeBench(cmd *cobra.Command, args []string) error {
return err
}

func showKeysStats(db *badger.DB) {
var (
internalKeyCount uint32
moveKeyCount uint32
invalidKeyCount uint32
validKeyCount uint32
)

txn := db.NewTransaction(false)
defer txn.Discard()

iopt := badger.DefaultIteratorOptions
iopt.AllVersions = true
iopt.InternalAccess = true
it := txn.NewIterator(iopt)
defer it.Close()

for it.Rewind(); it.Valid(); it.Next() {
i := it.Item()
if bytes.HasPrefix(i.Key(), []byte("!badger!")) {
internalKeyCount++
}
if bytes.HasPrefix(i.Key(), []byte("!badger!Move")) {
moveKeyCount++
}
if i.IsDeletedOrExpired() {
invalidKeyCount++
} else {
validKeyCount++
}
}
fmt.Printf("Valid Keys: %d Invalid Keys: %d Move Keys:"+
" %d Internal Keys: %d\n", validKeyCount, invalidKeyCount,
moveKeyCount, internalKeyCount)
}

func reportStats(c *y.Closer, db *badger.DB) {
defer c.Done()

Expand All @@ -309,28 +344,8 @@ func reportStats(c *y.Closer, db *badger.DB) {
case <-c.HasBeenClosed():
return
case <-t.C:
txn := db.NewTransaction(false)
defer txn.Discard()

iopt := badger.DefaultIteratorOptions
iopt.AllVersions = true
iopt.InternalAccess = true

it := txn.NewIterator(iopt)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
i := it.Item()
if bytes.HasPrefix(i.Key(), []byte("!badger!")) {
internalKeyCount++
}
if bytes.HasPrefix(i.Key(), []byte("!badger!Move")) {
moveKeyCount++
}
if i.IsDeletedOrExpired() {
invalidKeyCount++
} else {
validKeyCount++
}
if showKeysCount {
showKeysStats(db)
}

// fetch directory contents
Expand Down Expand Up @@ -365,9 +380,6 @@ func reportStats(c *y.Closer, db *badger.DB) {
fmt.Printf("Time elapsed: %s, bytes written: %s, speed: %s/sec, "+
"entries written: %d, speed: %d/sec, gcSuccess: %d\n", y.FixedDuration(time.Since(startTime)),
humanize.Bytes(sz), humanize.Bytes(bytesRate), entries, entriesRate, gcSuccess)
fmt.Printf("Valid Keys Count: %d\nInvalid Keys Count: %d\nMove Keys Count: %d\n"+
"Internal Keys Count: %d\n", validKeyCount, invalidKeyCount, moveKeyCount,
internalKeyCount)
}
}
}
Expand Down

0 comments on commit d31355d

Please sign in to comment.