Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit a034726

Browse files
authored
Merge pull request etcd-io#13679 from lavacat/defrag-bopts-fix
server/storage/backend: restore original bolt db options after defrag
2 parents 310de9b + 01347a8 commit a034726

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

server/storage/backend/backend.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ type backend struct {
9999
// mlock prevents backend database file to be swapped
100100
mlock bool
101101

102-
mu sync.RWMutex
103-
db *bolt.DB
102+
mu sync.RWMutex
103+
bopts *bolt.Options
104+
db *bolt.DB
104105

105106
batchInterval time.Duration
106107
batchLimit int
@@ -184,7 +185,8 @@ func newBackend(bcfg BackendConfig) *backend {
184185
// In future, may want to make buffering optional for low-concurrency systems
185186
// or dynamically swap between buffered/non-buffered depending on workload.
186187
b := &backend{
187-
db: db,
188+
bopts: bopts,
189+
db: db,
188190

189191
batchInterval: bcfg.BatchInterval,
190192
batchLimit: bcfg.BatchLimit,
@@ -510,13 +512,7 @@ func (b *backend) defrag() error {
510512
b.lg.Fatal("failed to rename tmp database", zap.Error(err))
511513
}
512514

513-
defragmentedBoltOptions := bolt.Options{}
514-
if boltOpenOptions != nil {
515-
defragmentedBoltOptions = *boltOpenOptions
516-
}
517-
defragmentedBoltOptions.Mlock = b.mlock
518-
519-
b.db, err = bolt.Open(dbp, 0600, &defragmentedBoltOptions)
515+
b.db, err = bolt.Open(dbp, 0600, b.bopts)
520516
if err != nil {
521517
b.lg.Fatal("failed to open database", zap.String("path", dbp), zap.Error(err))
522518
}

server/storage/backend/backend_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,17 @@ func TestBackendBatchIntervalCommit(t *testing.T) {
122122
}
123123

124124
func TestBackendDefrag(t *testing.T) {
125-
b, _ := betesting.NewDefaultTmpBackend(t)
125+
bcfg := backend.DefaultBackendConfig()
126+
// Make sure we change BackendFreelistType
127+
// The goal is to verify that we restore config option after defrag.
128+
if bcfg.BackendFreelistType == bolt.FreelistMapType {
129+
bcfg.BackendFreelistType = bolt.FreelistArrayType
130+
} else {
131+
bcfg.BackendFreelistType = bolt.FreelistMapType
132+
}
133+
134+
b, _ := betesting.NewTmpBackendFromCfg(t, bcfg)
135+
126136
defer betesting.Close(t, b)
127137

128138
tx := b.BatchTx()
@@ -168,6 +178,10 @@ func TestBackendDefrag(t *testing.T) {
168178
if nsize >= size {
169179
t.Errorf("new size = %v, want < %d", nsize, size)
170180
}
181+
db := backend.DbFromBackendForTest(b)
182+
if db.FreelistType != bcfg.BackendFreelistType {
183+
t.Errorf("db FreelistType = [%v], want [%v]", db.FreelistType, bcfg.BackendFreelistType)
184+
}
171185

172186
// try put more keys after shrink.
173187
tx = b.BatchTx()

0 commit comments

Comments
 (0)