How to avoid get ErrBlockedWrites when call DB.DropPrefix and DB.Update on two threads?

how to avoid get ErrBlockedWrites when call DB.DropPrefix and DB.Update on two threads?

  • I have use a sync.Mutex to avoid this problem,Is there any other good way to solve this problem?

  • sync.RWMutex? RLock to DB.Update , Lock to DB.DropPrefix?

  • retry in DB.Update?

How to avoid close of closed channel when DB.Flatten called in two threads?

panic: close of closed channel

goroutine 31 [running]:

github.com/dgraph-io/badger/y.(*Closer).Signal(...)

github.com/dgraph-io/badger/y/y.go:205

github.com/dgraph-io/badger/y.(*Closer).SignalAndWait(...)

github.com/dgraph-io/badger/y/y.go:232

github.com/dgraph-io/badger.(*DB).stopCompactions(0xc0000a6d80)

github.com/dgraph-io/badger/db.go:1352 +0x48

github.com/dgraph-io/badger.(*DB).Flatten(0xc0000a6d80, 0x2, 0x0, 0x0)

github.com/dgraph-io/badger/db.go:1381 +0x4e

Hey @HattieWebb,

how to avoid get ErrBlockedWrites when call DB.DropPrefix and DB.Update on two threads?

You cannot avoid the blocked writes error. The current implementation of drop prefix is a stop world operation which means we stop all the writes and then perform a drop prefix on it. We do plan on making the drop prefix asynchronous (drop prefixes in the background).

How to avoid close of closed channel when DB.Flatten called in two threads?

Flatten is also a stop-world operation and currently, you cannot call it from two threads. Why would you want to make two flatten calls at the same time? The second flatten call would be a no-op anyway.

I think we can improve some of the errors returned by badger. The current behavior is to panic but we should return appropriate errors.