server/storage/backend: restore original bolt db options after defrag

Problem: Defrag was implemented before custom bolt options were added.
Currently defrag doesn't restore backend options.
For example BackendFreelistType will be unset after defrag.

Solution: save bolt db options and use them in defrag.
dependabot/go_modules/go.uber.org/atomic-1.10.0
Bogdan Kanivets 2022-02-09 10:46:18 -08:00
parent 20c89df5e5
commit 01347a8f53
2 changed files with 21 additions and 11 deletions

View File

@ -99,8 +99,9 @@ type backend struct {
// mlock prevents backend database file to be swapped
mlock bool
mu sync.RWMutex
db *bolt.DB
mu sync.RWMutex
bopts *bolt.Options
db *bolt.DB
batchInterval time.Duration
batchLimit int
@ -184,7 +185,8 @@ func newBackend(bcfg BackendConfig) *backend {
// In future, may want to make buffering optional for low-concurrency systems
// or dynamically swap between buffered/non-buffered depending on workload.
b := &backend{
db: db,
bopts: bopts,
db: db,
batchInterval: bcfg.BatchInterval,
batchLimit: bcfg.BatchLimit,
@ -510,13 +512,7 @@ func (b *backend) defrag() error {
b.lg.Fatal("failed to rename tmp database", zap.Error(err))
}
defragmentedBoltOptions := bolt.Options{}
if boltOpenOptions != nil {
defragmentedBoltOptions = *boltOpenOptions
}
defragmentedBoltOptions.Mlock = b.mlock
b.db, err = bolt.Open(dbp, 0600, &defragmentedBoltOptions)
b.db, err = bolt.Open(dbp, 0600, b.bopts)
if err != nil {
b.lg.Fatal("failed to open database", zap.String("path", dbp), zap.Error(err))
}

View File

@ -122,7 +122,17 @@ func TestBackendBatchIntervalCommit(t *testing.T) {
}
func TestBackendDefrag(t *testing.T) {
b, _ := betesting.NewDefaultTmpBackend(t)
bcfg := backend.DefaultBackendConfig()
// Make sure we change BackendFreelistType
// The goal is to verify that we restore config option after defrag.
if bcfg.BackendFreelistType == bolt.FreelistMapType {
bcfg.BackendFreelistType = bolt.FreelistArrayType
} else {
bcfg.BackendFreelistType = bolt.FreelistMapType
}
b, _ := betesting.NewTmpBackendFromCfg(t, bcfg)
defer betesting.Close(t, b)
tx := b.BatchTx()
@ -168,6 +178,10 @@ func TestBackendDefrag(t *testing.T) {
if nsize >= size {
t.Errorf("new size = %v, want < %d", nsize, size)
}
db := backend.DbFromBackendForTest(b)
if db.FreelistType != bcfg.BackendFreelistType {
t.Errorf("db FreelistType = [%v], want [%v]", db.FreelistType, bcfg.BackendFreelistType)
}
// try put more keys after shrink.
tx = b.BatchTx()