Skip to content

Commit

Permalink
feat: preload freelist to optimize the boltdb open
Browse files Browse the repository at this point in the history
  • Loading branch information
absolute8511 committed Aug 28, 2020
1 parent 5b51933 commit 0a0047f
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions nsqd/delay_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func getDefaultBoltDbOptions(readOnly bool) *bolt.Options {
return &bolt.Options{
Timeout: time.Second,
ReadOnly: readOnly,
FreelistType: bolt.FreelistMapType,
FreelistType: bolt.FreelistArrayType,
NoFreelistSync: true,
}
}
Expand Down Expand Up @@ -584,6 +584,17 @@ func (q *DelayQueue) ReopenWithEmpty() error {
return nil
}

func preloadDBAndOptimizeOpen(dbPath string) error {
ro := getDefaultBoltDbOptions(false)
// use this to scan freelist and sync to disk
ro.NoFreelistSync = false
_, err := bolt.Open(dbPath, 0644, ro)
if err != nil {
return err
}
return nil
}

func (q *DelayQueue) RestoreKVStoreFrom(body io.Reader) error {
buf := make([]byte, 8)
n, err := body.Read(buf)
Expand All @@ -594,7 +605,7 @@ func (q *DelayQueue) RestoreKVStoreFrom(body io.Reader) error {
return errors.New("unexpected length for body length")
}
bodyLen := int64(binary.BigEndian.Uint64(buf))
tmpPath := path.Join(q.dataPath, getDelayQueueDBName(q.tname, q.partition)+"-tmp.restore")
tmpPath := fmt.Sprintf("%s-tmp.restore.%d", q.getStore().Path(), time.Now().UnixNano())
err = os.Remove(tmpPath)
if err != nil {
if !os.IsNotExist(err) {
Expand All @@ -620,6 +631,10 @@ func (q *DelayQueue) RestoreKVStoreFrom(body io.Reader) error {
return err
}

err = preloadDBAndOptimizeOpen(tmpPath)
if err != nil {
return err
}
q.compactMutex.Lock()
defer q.compactMutex.Unlock()
kvPath := path.Join(q.dataPath, getDelayQueueDBName(q.tname, q.partition))
Expand Down Expand Up @@ -1442,6 +1457,8 @@ func (q *DelayQueue) compactStore(force bool) error {
tmpPath := fmt.Sprintf("%s-tmp.compact.%d", src.Path(), time.Now().UnixNano())
// Open destination database.
ro := getDefaultBoltDbOptions(false)
// we need sync free list to speed up the reopen which will hold write lock
ro.NoFreelistSync = false
dst, err := bolt.Open(tmpPath, 0644, ro)
if err != nil {
return err
Expand Down

0 comments on commit 0a0047f

Please sign in to comment.