server: reapply `Mlock` flag after defrag

release-3.5
wpedrak 2021-05-03 11:01:02 +02:00
parent c46e96d519
commit 6623c008ee
1 changed files with 11 additions and 2 deletions

View File

@ -91,6 +91,8 @@ type backend struct {
commits int64
// openReadTxN is the number of currently open read transactions in the backend
openReadTxN int64
// mlock prevents backend database file to be swapped
mlock bool
mu sync.RWMutex
db *bolt.DB
@ -171,6 +173,7 @@ func newBackend(bcfg BackendConfig) *backend {
batchInterval: bcfg.BatchInterval,
batchLimit: bcfg.BatchLimit,
mlock: bcfg.Mlock,
readTx: &readTx{
baseReadTx: baseReadTx{
@ -381,7 +384,7 @@ func (b *backend) defrag() error {
if boltOpenOptions != nil {
options = *boltOpenOptions
}
options.OpenFile = func(path string, i int, mode os.FileMode) (file *os.File, err error) {
options.OpenFile = func(_ string, _ int, _ os.FileMode) (file *os.File, err error) {
return temp, nil
}
// Don't load tmp db into memory regardless of opening options
@ -428,7 +431,13 @@ func (b *backend) defrag() error {
b.lg.Fatal("failed to rename tmp database", zap.Error(err))
}
b.db, err = bolt.Open(dbp, 0600, boltOpenOptions)
defragmentedBoltOptions := bolt.Options{}
if boltOpenOptions != nil {
defragmentedBoltOptions = *boltOpenOptions
}
defragmentedBoltOptions.Mlock = b.mlock
b.db, err = bolt.Open(dbp, 0600, &defragmentedBoltOptions)
if err != nil {
b.lg.Fatal("failed to open database", zap.String("path", dbp), zap.Error(err))
}