What version of Go are you using (go version
)?
go1.16 linux/amd64
What operating system are you using?
Linux (Ubuntu 20.04)
What version of Badger are you using?
Does this issue reproduce with the latest master?
Yes
Steps to Reproduce the issue
What Badger options were set?
badgerDBParentDirectory := "./BadgerPersistentKeyValueDB/"
if _, err := os.Stat(badgerDBParentDirectory); os.IsNotExist(err) {
_ = os.Mkdir(badgerDBParentDirectory, os.ModePerm)
}
options := badger.DefaultOptions(badgerDBParentDirectory)
options.Logger = nil // Comment this out to print badger DB analytics
options.NumVersionsToKeep = 1
var err error
badgerDB, err = badger.Open(options)
if err != nil {
log.Fatal(err)
}
What did you do?
I use below three functions to store a single key value pair, delete a single key (and corresponding value) and return “n” number of key value pair from the DB. The GetNKeyValues function still seem to be returning deleted keys value pair in spite of all checks in place. This seem to happen only during the stress testing, does not happen when testing with few key value pair or low system load.
func StoreKeyValue(key, value []byte) error {
err := badgerDB.Update(func(txn *badger.Txn) error {
e := badger.NewEntry(key, value).WithTTL(time.Hour * 48)
err := txn.SetEntry(e)
return err
})
return err
}
func DeleteKeyValue(key []byte) error {
err := badgerDB.Update(func(txn *badger.Txn) error {
err := txn.Delete(key)
return err
})
return err
}
func GetNKeyValues(numberOfKeys int64) ([][]byte, [][]byte, error) { // key, value, error
var outputKeySlice, outputValuesSlice [][]byte
err := badgerDB.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = true
opts.PrefetchSize = int(numberOfKeys)
it := txn.NewIterator(opts)
defer it.Close()
var keyCount int64 = 0
for it.Rewind(); it.Valid() && keyCount < numberOfKeys; it.Next() {
item := it.Item()
if item.IsDeletedOrExpired() {
continue
}
keyCount++
outputKeySlice = append(outputKeySlice, item.Key())
err := item.Value(func(v []byte) error {
value := make([]byte, len(v))
copy(value, v)
outputValuesSlice = append(outputValuesSlice, value)
return nil
})
if err != nil {
return err
}
}
return nil
})
return outputKeySlice, outputValuesSlice, err
}
What did you expect to see?
I expected to only get non-deleted key value from the GetNKeyValues function
What did you see instead?
The deleted keys pair are being returned and not sure what am I missing here.