Hello all and sorry if my question is not at the right place.
I reposted from Github since I realized it was not the appropriate place.
Its related to a miss usage of the library and probably not a bug
What version of Go are you using ( go version
)?
$ go version 1.14
What operating system are you using?
Windows 10
What version of Badger are you using?
Last one from master (v2 I guess)
Does this issue reproduce with the latest master?
Yes
Steps to Reproduce the issue
I uses badger to create a DB and store items. I created a function that use a txn.View to Get and it provided its key. This function defer Close() the db after the transaction.
func ReadStatsFromDb(dbname string) (*models.Stats, error) {
db, err := OpenDB("databases/stats")
if err != nil {
return nil, errors.New("databases: could not read stats from db - " + err.Error())
}
defer db.Close()
var data []byte
err = db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(dbname))
if err != nil {
return err
}
data, err = item.ValueCopy(nil)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, errors.New("databases: could not read stats from db - " + err.Error())
}
stats, err := helpers.DecodeStats(data)
if err != nil {
return nil, errors.New("databases: could not read stats from db - " + err.Error())
}
return stats, nil
}
I then exposed this function using Beego as an API that responds to GET call on localhost:8080/stats/:key (where key il the key to look for in the badger DB)
Lastly the same Beego runtime serve a HTML page on localhost:8080/ with javascrit that pulls data from the API.
In the javascript code served with the html is some code:
["key1", "key2", "key3"].forEach((key) => {
fetch("/stats/${key}").then(Do some stuff..)
}
the forEach function iter over the value in the array and execute the request to the API for each of the key value. This process is sequential…
The thing is I can retrieve the first value… but then the other fail:
Cannot create lock file …\LOCK Another process is using this Badger
If I introduce delay in the forEach loop it solves the issue after at least 200ms delay between each call…
My question how would you tackle this issue? Should I not close the DB between each search for key? I suppose it has to do with the lock and file system and since the DB is on my slow hard drive the OS don’t have time to suppress the lock file between each call? Is there a specific setting to change this behavior?
What Badger options were set?
options := badger.DefaultOptions(path)
options.Truncate = true
What did you see instead?
Here I iterate over 4 keys in JS and the first one succeeded, the next two failed LOCK issue and the last succeeded again…
Here is the logger output :
badger 2020/04/14 18:40:24 INFO: All 1 tables opened in 0s
badger 2020/04/14 18:40:24 INFO: Replaying file id: 0 at offset: 421594
badger 2020/04/14 18:40:24 INFO: Replay took: 0s
badger 2020/04/14 18:40:24 DEBUG: Value log discard stats empty
badger 2020/04/14 18:40:24 INFO: Got compaction priority: {level:0 score:1.73 dropPrefix:}
badger 2020/04/14 18:40:24 INFO: All 1 tables opened in 1ms
badger 2020/04/14 18:40:24 INFO: Replaying file id: 0 at offset: 421594
badger 2020/04/14 18:40:24 INFO: Replay took: 0s
badger 2020/04/14 18:40:24 DEBUG: Value log discard stats empty
badger 2020/04/14 18:40:24 INFO: Got compaction priority: {level:0 score:1.73 dropPrefix:}
Thank you for any help provided.
Best regards;