Skip to content

Commit

Permalink
Use MAP_POPULATE for our bbolt mmaps (#13573)
Browse files Browse the repository at this point in the history
* Use MAP_POPULATE for our bbolt mmaps, assuming the files fit in memory.  This should improve startup times when freelist sync is disabled.
  • Loading branch information
ncabatoff committed Jan 11, 2022
1 parent 686996c commit add0a56
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/13573.txt
@@ -0,0 +1,3 @@
```release-note:bug
storage/raft: On linux, use map_populate for bolt files to improve startup time.
```
36 changes: 36 additions & 0 deletions physical/raft/bolt_linux.go
@@ -0,0 +1,36 @@
package raft

import (
"context"
"os"

"github.com/shirou/gopsutil/mem"
"golang.org/x/sys/unix"
)

func init() {
getMmapFlags = getMmapFlagsLinux
}

func getMmapFlagsLinux(dbPath string) int {
if os.Getenv("VAULT_RAFT_DISABLE_MAP_POPULATE") != "" {
return 0
}
stat, err := os.Stat(dbPath)
if err != nil {
return 0
}
size := stat.Size()

v, err := mem.VirtualMemoryWithContext(context.Background())
if err != nil {
return 0
}

// We won't worry about swap, since we already tell people not to use it.
if v.Total > uint64(size) {
return unix.MAP_POPULATE
}

return 0
}
2 changes: 1 addition & 1 deletion physical/raft/fsm.go
Expand Up @@ -168,12 +168,12 @@ func (f *FSM) openDBFile(dbPath string) error {
}
}

freelistType, noFreelistSync := freelistOptions()
start := time.Now()
boltDB, err := bolt.Open(dbPath, 0o600, &bolt.Options{
Timeout: 1 * time.Second,
FreelistType: freelistType,
NoFreelistSync: noFreelistSync,
MmapFlags: getMmapFlags(dbPath),
})
if err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion physical/raft/raft.go
Expand Up @@ -40,6 +40,8 @@ const EnvVaultRaftNodeID = "VAULT_RAFT_NODE_ID"
// EnvVaultRaftPath is used to fetch the path where Raft data is stored from the environment.
const EnvVaultRaftPath = "VAULT_RAFT_PATH"

var getMmapFlags = func(string) int { return 0 }

// Verify RaftBackend satisfies the correct interfaces
var (
_ physical.Backend = (*RaftBackend)(nil)
Expand Down Expand Up @@ -364,12 +366,14 @@ func NewRaftBackend(conf map[string]string, logger log.Logger) (physical.Backend
}

// Create the backend raft store for logs and stable storage.
dbPath := filepath.Join(path, "raft.db")
freelistType, noFreelistSync := freelistOptions()
raftOptions := raftboltdb.Options{
Path: filepath.Join(path, "raft.db"),
Path: dbPath,
BoltOptions: &bolt.Options{
FreelistType: freelistType,
NoFreelistSync: noFreelistSync,
MmapFlags: getMmapFlags(dbPath),
},
}
store, err := raftboltdb.New(raftOptions)
Expand Down

0 comments on commit add0a56

Please sign in to comment.