server: reapply Mlock flag after defrag

This commit is contained in:
wpedrak
2021-05-03 11:01:02 +02:00
parent c46e96d519
commit 6623c008ee

View File

@@ -91,6 +91,8 @@ type backend struct {
commits int64 commits int64
// openReadTxN is the number of currently open read transactions in the backend // openReadTxN is the number of currently open read transactions in the backend
openReadTxN int64 openReadTxN int64
// mlock prevents backend database file to be swapped
mlock bool
mu sync.RWMutex mu sync.RWMutex
db *bolt.DB db *bolt.DB
@@ -171,6 +173,7 @@ func newBackend(bcfg BackendConfig) *backend {
batchInterval: bcfg.BatchInterval, batchInterval: bcfg.BatchInterval,
batchLimit: bcfg.BatchLimit, batchLimit: bcfg.BatchLimit,
mlock: bcfg.Mlock,
readTx: &readTx{ readTx: &readTx{
baseReadTx: baseReadTx{ baseReadTx: baseReadTx{
@@ -381,7 +384,7 @@ func (b *backend) defrag() error {
if boltOpenOptions != nil { if boltOpenOptions != nil {
options = *boltOpenOptions 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 return temp, nil
} }
// Don't load tmp db into memory regardless of opening options // 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.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 { if err != nil {
b.lg.Fatal("failed to open database", zap.String("path", dbp), zap.Error(err)) b.lg.Fatal("failed to open database", zap.String("path", dbp), zap.Error(err))
} }